給元素固有屬性賦值
例如,你可以依照瀏覽器的大小來安置一個元素的位置。
#myDiv {
position: absolute;
width: 100px;
height: 100px;
left: expression(document.body.offsetWidth - 110 "px");
top: expression(document.body.offsetHeight - 110 "px");
background: red;
}
給元素自定義屬性賦值
例如,消除頁面上的鏈接虛線框。 通常的做法是:
<a href="link1.htm" onfocus="this.blur()">link1</a>
<a href="link2.htm" onfocus="this.blur()">link2</a>
<a href="link3.htm" onfocus="this.blur()">link3</a>
粗看或許還體現(xiàn)不出采用expression的優(yōu)勢,但假如你的頁面上有幾十甚至上百個鏈接,這時的你難道還會機(jī)械式地Ctrl C,Ctrl V么,何況兩者一比較,哪個產(chǎn)生的冗余代碼更多呢?
采用expression的做法如下:
<style type="text/css">
a {star : expression(onfocus=this.blur)}
</style>
<a href="link1.htm">link1</a>
<a href="link2.htm">link2</a>
<a href="link3.htm">link3</a>
說明:里面的star就是自己任意定義的屬性,你可以隨自己喜好另外定義,接著包含在expression()里的語句就是JS腳本,在自定義屬性與expression之間可別忘了還有一個引號,因?yàn)閷?shí)質(zhì)還是CSS,所以放在style標(biāo)簽內(nèi),而非s cript內(nèi)。OK,這樣就很輕易地用一句話實(shí)現(xiàn)了頁面中的鏈接虛線框的消除。不過你先別自得,假如觸發(fā)的特效是CSS的屬性變化,那么出來的結(jié)果會跟你的本意有差別。例如你想隨鼠標(biāo)的移進(jìn)移出而改變頁面中的文本框顏色更改,你可能想當(dāng)然的會認(rèn)為應(yīng)該寫為
<style type="text/css">
input
{star : expression(onmouseover=this.style.backgroundColor="#FF0000";
onmouseout=this.style.backgroundColor="#FFFFFF")}
</style>
<style type="text/css">
input {star : expression(onmouseover=this.style.backgroundColor="#FF0000";
onmouseout=this.style.backgroundColor="#FFFFFF")}
</style>
<input type="text">
<input type="text">
<input type="text">
可結(jié)果卻是出現(xiàn)腳本出錯,正確的寫法應(yīng)該把CSS樣式的定義寫進(jìn)函數(shù)內(nèi),如下所示:
<style type="text/css">
input {star : expression(onmouseover=function()
{this.style.backgroundColor="#FF0000"},
onmouseout=function(){this.style.backgroundColor="#FFFFFF"}) }
</style>
<input type="text">
<input type="text">
<input type="text">
注重
不是非常需要,一般不建議使用expression,因?yàn)閑xpression對瀏覽器資源要求比較高。
新聞熱點(diǎn)
疑難解答