addRule在firefox下的兼容寫法
更新時間:2006年11月30日 00:00:00 作者:
現(xiàn)在用腳本控制 html 元素樣式的方法真的很多很多。
對單個元素可以直接 element.style.display=......修改一個樣式,也可以 element.className=...修改它的多個樣式。
對于多個元素修改樣式可以用腳本直接 import css文件。
目前項目中有這么一個需求。 要求改變某 div下所有子孫結(jié)點(diǎn)的樣式,但不改變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é)點(diǎn)的樣式,但不改變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新功能介紹之findLast()和findLastIndex()
最近工作中遇到了一個關(guān)于查找數(shù)組里面的目標(biāo)元素的方法,所以下面這篇文章主要給大家介紹了關(guān)于JavaScript新功能之findLast()?和findLastIndex()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04JavaScript中判斷整字類型最簡潔的實現(xiàn)方法
這篇文章主要介紹了JavaScript中判斷整字類型最簡潔的實現(xiàn)方法,本文給出多個判斷整數(shù)的方法,最后總結(jié)出一個最短、最簡潔的實現(xiàn)方法,需要的朋友可以參考下2014-11-11