CSS2.0中expression如果使用

发布时间:2021-07-09 16:04:55 作者:Leah
来源:亿速云 阅读:130

本篇文章为大家展示了CSS2.0中expression如果使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

CSS2.0中的expression应用

IE5及其以后版本支持在CSS中使用expression,用来把CSS属性和Javascript表达式关联起来,这里的CSS属性可以是元素固有的属性,也可以是自定义属性。就是说CSS属性后面可以是一段Javascript表达式,CSS属性的值等于Javascript表达式计算的结果。在表达式中可以直接引用元素自身的属性和方法,也可以使用其他浏览器对象。这个表达式就好像是在这个元素的一个成员函数中一样。

给元素固有属性赋值

例如,你可以依照浏览器的大小来安置一个元素的位置。

ExampleSourceCode

#myDiv{  position:absolute;  width:100px;  height:100px;  left:expression(document.body.offsetWidth-110+\"px\");  top:expression(document.body.offsetHeight-110+\"px\");  background:red;  }

给元素自定义属性赋值

例如,消除页面上的链接虚线框。通常的做法是:

ExampleSourceCode

<ahrefahref=\"link1.htm\"onfocus=\"this.blur()\">link1</a> <ahrefahref=\"link2.htm\"onfocus=\"this.blur()\">link2</a> <ahrefahref=\"link3.htm\"onfocus=\"this.blur()\">link3</a>

粗看或许还体现不出采用expression的优势,但如果你的页面上有几十甚至上百个链接,这时的你难道还会机械式地Ctrl+C,Ctrl+V么,何况两者一比较,哪个产生的冗余代码更多呢?

◆采用expression的做法如下:

ExampleSourceCode

<styletypestyletype=\"text/css\"> a{star:expression(onfocus=this.blur)}  </style> <ahrefahref=\"link1.htm\">link1</a> <ahrefahref=\"link2.htm\">link2</a> <ahrefahref=\"link3.htm\">link3</a>

说明:里面的star就是自己任意定义的属性,你可以随自己喜好另外定义,接着包含在expression()里的语句就是JS脚本,在自定义属性与expression之间可别忘了还有一个引号,因为实质还是CSS,所以放在style标签内,而非script内。OK,这样就很容易地用一句话实现了页面中的链接虚线框的消除。不过你先别得意,如果触发的特效是CSS的属性变化,那么出来的结果会跟你的本意有差别。例如你想随鼠标的移进移出而改变页面中的文本框颜色更改,你可能想当然的会认为应该写为:

ExampleSourceCode

<styletypestyletype=\"text/css\"> input  {star:expression(onmouseover=this.style.backgroundColor=\"#FF0000\";  onmouseout=this.style.backgroundColor=\"#FFFFFF\")}  </style> <styletypestyletype=\"text/css\"> input{star:expression(onmouseover=this.style.backgroundColor=\"#FF0000\";  onmouseout=this.style.backgroundColor=\"#FFFFFF\")}  </style> <inputtypeinputtype=\"text\"> <inputtypeinputtype=\"text\"> <inputtypeinputtype=\"text\">

可结果却是出现脚本出错,正确的写法应该把CSS样式的定义写进函数内,如下所示:

ExampleSourceCode

<styletypestyletype=\"text/css\"> input{star:expression(onmouseover=function()  {this.style.backgroundColor=\"#FF0000\"},  onmouseout=function(){this.style.backgroundColor=\"#FFFFFF\"})}  </style> <inputtypeinputtype=\"text\"> <inputtypeinputtype=\"text\"> <inputtypeinputtype=\"text\">

注意:不是非常需要,一般不建议使用expression,因为expression对浏览器资源要求比较高!

上述内容就是CSS2.0中expression如果使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Expression:invalid comarator怎么办
  2. CSS中如何使用expression表达式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css expression

上一篇:集成spring+springmvc+mybatis时无法访问到controller层的原因是什么

下一篇:Centos7 Gitea的安装教程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》