CSS中的expression
给元素固有属性赋值
例如,你可以依照浏览器之大小来安置一个元素之位置。
#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>
粗看或许还体现不出采用expression之优势,但如果你之页面上有几十甚至上百个链接,这时之你难道还会机械式之复制\粘贴么,何况两者一比较,哪个产生之冗余代码更多呢?
采用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之间可别忘了还有一个引号,因为实质还是CSS,所以放在style标签内,而非s cript内。OK,这样就很容易之用一句话实现了页面中之链接虚线框之消除。不过你先别得意,如果触发之特效是CSS之属性变化,那么出来之结果会跟你之本意有差别。例如你想随鼠标之移进移出而改变页面中之文本框颜色更改,你可能想当然之会认为应该写为
<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">
可结果却是出现脚本出错,正确之写法应该把CSS样式之定义写进函数内,如下所示:
<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,因为expression对浏览器资源要求比较高。