firefox火狐瀏覽器與與ie兼容的2個問題總結(jié)
更新時間:2010年07月20日 00:40:06 作者:
這幾天遇到幾個頭疼的火狐與ie兼容問題整理下來,希望對需要的朋友有所幫助。
1:rules與cssRules區(qū)別:
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)) ;
}
}
我是這樣加了一個方法解決這個問題的。。
2:火狐和ie中獲得背景色問題(getComputedStyle與currentStyle的區(qū)別)
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);
}
}
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;
}
記住 火狐獲得的rgbstr是rgb的因此我還要轉(zhuǎn)成16進(jìn)制的。我也整理了一個很笨的轉(zhuǎn)換方法再上面望打擊拍磚!
復(fù)制代碼 代碼如下:
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)) ;
}
}
我是這樣加了一個方法解決這個問題的。。
2:火狐和ie中獲得背景色問題(getComputedStyle與currentStyle的區(qū)別)
復(fù)制代碼 代碼如下:
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);
}
}
復(fù)制代碼 代碼如下:
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;
}
記住 火狐獲得的rgbstr是rgb的因此我還要轉(zhuǎn)成16進(jìn)制的。我也整理了一個很笨的轉(zhuǎn)換方法再上面望打擊拍磚!
相關(guān)文章
詳解layui?laydate選擇時間的回調(diào)方法
這篇文章主要介紹了layui?laydate選擇時間的回調(diào)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
JS實(shí)現(xiàn)OCX控件的事件響應(yīng)示例
JS支持OCX控件的事件(event),當(dāng)OCX控件定義的事件發(fā)生時,JS可以捕獲該事件并對事件進(jìn)行相應(yīng)的處理2014-09-09
一文詳解Promise.race()方法功能及應(yīng)用場景
這篇文章主要為大家介紹了Promise.race()方法功能及應(yīng)用場景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

