addRule在firefox下的兼容寫法
更新時間:2006年11月30日 00:00:00 作者:
現(xiàn)在用腳本控制 html 元素樣式的方法真的很多很多。
對單個元素可以直接 element.style.display=......修改一個樣式,也可以 element.className=...修改它的多個樣式。
對于多個元素修改樣式可以用腳本直接 import css文件。
目前項目中有這么一個需求。 要求改變某 div下所有子孫結(jié)點的樣式,但不改變div本身樣式。
我當(dāng)時想都沒有想就留下了一下代碼:
<style>
span{background-color:blue;}
input{background-color:gray}
button{margin-top:70px;width:50px}
</style>
<!-- by Go_Rush(阿舜) from http://ashun.cnblogs.com/ --->
<div id="a1">
divdiv
<span>
spanspan
<h2>ttttt</h2>
</span>
<input>
</div>
<button onclick='document.styleSheets[0].addRule("#a1 *","background-color:red")'>轉(zhuǎn)</button>
這段代碼一直工作得很好,直到有一天我同事告訴我,我的模塊讓他的瀏覽器崩潰了。
我打著手電筒,拿著放大鏡在近兩千多行的代碼中找了又找。.
根本找不到問題出在哪里,而且當(dāng)時對于上面的代碼絲毫沒有懷疑過。
我們div 的id 是從數(shù)據(jù)庫里面直接讀取的,為數(shù)字類型,比如 <div id="345"></div>等等。
對于id 能取什么字符,我以前在 ie6和 ff1.5中測試過,
<div id="阿舜"></div>
<div id="???"></div>
<div id="-1"></div>
這樣的id設(shè)置,都可以用
document.getElementById("阿舜")
document.getElementById("???"),
document.getElementById("-1"),
讀出來不會有問題,更何況是純數(shù)字呢.
最后經(jīng)過反復(fù)調(diào)試,找到這個罪魁禍?zhǔn)?nbsp;addRule.
document.styleSheets[0].addRule("#a1 *","background-color:red")
document.styleSheets[0].addRule("#123 *","background-color:red")
都沒有任何問題
下面這行代碼卻會讓ie死掉. 內(nèi)存和虛擬內(nèi)存一直暴漲,我512的內(nèi)存,一下子xp就提示虛擬內(nèi)存不夠。
document.styleSheets[0].addRule("#-1 *","background-color:red");
同樣,在 ff1.5中,將導(dǎo)致函數(shù)出錯,但不會崩潰。
總結(jié):
1。 ie,ff對 id 的取值相當(dāng)寬容,id幾乎可以是任何字符,中文,英文,字母,數(shù)字,負(fù)數(shù),特殊字符
2。 但是 addRule 函數(shù)中, id為負(fù)數(shù)時,在 ie和ff都會失敗,而且ie會崩潰。
附:
addRule在 ff中的兼容寫法
<style>
span{}{background-color:blue;}
input{}{background-color:gray}
button{}{margin-top:70px;width:50px}
</style>
<!-- by Go_Rush(阿舜) from http://ashun.cnblogs.com/ --->
<div id="a1">
divdiv
<span>
spanspan
<h2>ttttt</h2>
</span>
<input>
</div>
<script>
function f(){
var rule="#a1 *{background-color:red}";
var index=document.styleSheets[0].cssRules.length;
document.styleSheets[0].insertRule(rule, index);
}
</script>
<button onclick=f()>轉(zhuǎn)</button>
對單個元素可以直接 element.style.display=......修改一個樣式,也可以 element.className=...修改它的多個樣式。
對于多個元素修改樣式可以用腳本直接 import css文件。
目前項目中有這么一個需求。 要求改變某 div下所有子孫結(jié)點的樣式,但不改變div本身樣式。
我當(dāng)時想都沒有想就留下了一下代碼:
復(fù)制代碼 代碼如下:
<style>
span{background-color:blue;}
input{background-color:gray}
button{margin-top:70px;width:50px}
</style>
<!-- by Go_Rush(阿舜) from http://ashun.cnblogs.com/ --->
<div id="a1">
divdiv
<span>
spanspan
<h2>ttttt</h2>
</span>
<input>
</div>
<button onclick='document.styleSheets[0].addRule("#a1 *","background-color:red")'>轉(zhuǎn)</button>
這段代碼一直工作得很好,直到有一天我同事告訴我,我的模塊讓他的瀏覽器崩潰了。
我打著手電筒,拿著放大鏡在近兩千多行的代碼中找了又找。.
根本找不到問題出在哪里,而且當(dāng)時對于上面的代碼絲毫沒有懷疑過。
我們div 的id 是從數(shù)據(jù)庫里面直接讀取的,為數(shù)字類型,比如 <div id="345"></div>等等。
對于id 能取什么字符,我以前在 ie6和 ff1.5中測試過,
復(fù)制代碼 代碼如下:
<div id="阿舜"></div>
<div id="???"></div>
<div id="-1"></div>
這樣的id設(shè)置,都可以用
document.getElementById("阿舜")
document.getElementById("???"),
document.getElementById("-1"),
讀出來不會有問題,更何況是純數(shù)字呢.
最后經(jīng)過反復(fù)調(diào)試,找到這個罪魁禍?zhǔn)?nbsp;addRule.
document.styleSheets[0].addRule("#a1 *","background-color:red")
document.styleSheets[0].addRule("#123 *","background-color:red")
都沒有任何問題
下面這行代碼卻會讓ie死掉. 內(nèi)存和虛擬內(nèi)存一直暴漲,我512的內(nèi)存,一下子xp就提示虛擬內(nèi)存不夠。
document.styleSheets[0].addRule("#-1 *","background-color:red");
同樣,在 ff1.5中,將導(dǎo)致函數(shù)出錯,但不會崩潰。
總結(jié):
1。 ie,ff對 id 的取值相當(dāng)寬容,id幾乎可以是任何字符,中文,英文,字母,數(shù)字,負(fù)數(shù),特殊字符
2。 但是 addRule 函數(shù)中, id為負(fù)數(shù)時,在 ie和ff都會失敗,而且ie會崩潰。
附:
addRule在 ff中的兼容寫法
復(fù)制代碼 代碼如下:
<style>
span{}{background-color:blue;}
input{}{background-color:gray}
button{}{margin-top:70px;width:50px}
</style>
<!-- by Go_Rush(阿舜) from http://ashun.cnblogs.com/ --->
<div id="a1">
divdiv
<span>
spanspan
<h2>ttttt</h2>
</span>
<input>
</div>
<script>
function f(){
var rule="#a1 *{background-color:red}";
var index=document.styleSheets[0].cssRules.length;
document.styleSheets[0].insertRule(rule, index);
}
</script>
<button onclick=f()>轉(zhuǎn)</button>
相關(guān)文章
javascript中String類的subString()方法和slice()方法
最近在看《Javascript高級程序設(shè)計》一書,在書中發(fā)現(xiàn)一些以前沒有接觸過的且比較實用的技巧和知識點,想通過博客記錄一下,以加深記憶。2011-05-05
元素未顯示設(shè)置width/height時IE中使用currentStyle獲取為auto
元素未顯示設(shè)置width/height時IE中無法使用currentStyle獲取,默認(rèn)獲取值為auto,需要的朋友可以參考下2014-05-05
如何使用big.js解決JavaScript浮點數(shù)精度丟失問題
最近在項目中涉及到金額從元轉(zhuǎn)為分(乘100即可),發(fā)現(xiàn)乘法居然也會有精度丟失的問題,關(guān)于浮點數(shù)計算精度丟失是很多語言都存在的問題,本文給大家分享使用big.js解決JavaScript浮點數(shù)精度丟失問題,感興趣的朋友一起看看吧2023-12-12
9行javascript代碼獲取QQ群成員具體實現(xiàn)
22 行 JavaScript 代碼實現(xiàn) QQ 群成員提取器,如果沒有達(dá)到效果可能原因一是QQ版本升級了,二是博客里面的代碼也有些繁瑣2013-10-10
使用濾鏡設(shè)置透明導(dǎo)致 IE 6/7/8/9 解析異常的解決方法
使用濾鏡設(shè)置透明導(dǎo)致 IE 6/7/8/9 解析異常的解決方法,需要的朋友可以參考下。2011-04-04
javascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解
下面小編就為大家?guī)硪黄猨avascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06

