Home  >  Article  >  Web Front-end  >  Summary of 2 issues between firefox and ie compatibility_javascript skills

Summary of 2 issues between firefox and ie compatibility_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:22:46836browse
1: The difference between rules and cssRules:
Copy code The code is as follows:

function addCSSRule(css,key,value){
//var css = document.styleSheets[document.styleSheets.length-1];
if(navigator.userAgent.indexOf("Firefox")>0 )
{
css.insertRule(key "{" value "}", css.cssRules.length)
}
else
{
css.addRules(key,value) ;
}
}
function removeCSSRule(key){
for(var i = 0; i < document.styleSheets.length; i ){
var css = document.styleSheets[ i];
navigator.userAgent.indexOf("Firefox")>0 ?
(function(){
for(var j = 0; j < css.cssRules.length; j ){
if(css.cssRules[j].selectorText==key){
css.deleteRule(j);
}
}
})() :
(css. removeRule(key)) ;
}
}

This is how I added a method to solve this problem. .
2: Problem with getting background color in Firefox and IE (difference between getComputedStyle and currentStyle)
Copy code The code is as follows:

function getCurrentStyle(oElement) {
if(navigator.userAgent.indexOf("Firefox")>0 ){
var rgbstr=document.defaultView. getComputedStyle(oElement,null).backgroundColor;
var strR;
if(rgbstr.toString().indexOf('(')>0 && rgbstr.toString().indexOf(')')> 0)
{
strR= rgbstr.toString().substring(parseInt(rgbstr.toString().indexOf('(') 1),rgbstr.toString().indexOf(')')). split(',');
}
return toHexColor(strR[0],strR[1],strR[2]).substring(1);
}
else{
return oElement.currentStyle.backgroundColor.trim().substring(1);
}
}

Copy code The code is as follows:

function toHexColor(r,g,b){
var hex='#';
var hexStr = '0123456789ABCDEF';
low = r % 16;
high = (r - low)/16;
hex =hexStr.charAt(high) hexStr.charAt(low);
low = g % 16;
high = (g - low)/16;
hex =hexStr.charAt(high) hexStr.charAt(low);
low = b % 16;
high = (b - low)/16;
hex =hexStr.charAt(high) hexStr.charAt(low);
return hex;
}

Remember that the rgbstr obtained by Firefox is rgb, so I also need Convert to hexadecimal. I also compiled a very stupid conversion method and then looked at it and hit it!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn