码迷,mamicode.com
首页 > Web开发 > 详细

去掉CSS中的表达式Expression

时间:2014-10-21 10:01:20      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   os   ar   使用   java   strong   sp   

 在IE中,CSS是可以嵌入js表达式的,可以在CSS类中定义,但是将含有表达CSS类从DOM对象中移除,样式表达式是不会失效的。

经过研究找到了答案,需要使用js调用style对象的removeExpression()方法才可去除。

下面是方法的说明:

 

语法

bSuccess = object.removeExpression(sPropertyName)

参数

sPropertyName Required. String that specifies the name of the property from which to remove an expression.

返回值

Returns one of the following possible values:

true The expression was successfully removed.
false The expression was not removed.

下面给出一个实用例子:利用样式限制图片最大宽度,通过一个按钮切换实际大小与缩放大小。

 

CSS类,作用:限制图片的最大宽度,超出指定宽度等比缩小。

Css代码  bubuko.com,布布扣
  1. .wrap {  
  2.     border:none;  
  3.     max-width:730px;  
  4.     height:auto;  
  5.     width:expression(this.width>730?"730px":true);/*ie6补丁*/  
  6.     }  

 

 图片<img>

Html代码  bubuko.com,布布扣
  1. <img id="chart_img" class="chart" src="bond_big.jpg"/>  

 

切换按钮

Html代码  bubuko.com,布布扣
  1. <span id="imgctr" style="cursor: pointer;display: block" onclick="showOrigin()">[原始大小]</span>  

 

js代码(需要jQuery)

Java代码  bubuko.com,布布扣
  1. function showOrigin(){  
  2.     $(‘#chart_img‘).toggleClass("chart");  
  3.       
  4.     if($(‘#chart_img‘).attr("class")){  
  5.         $(‘#imgctr‘).text("[原始大小]");  
  6.     }else{        
  7.         $(‘#imgctr‘).text("[缩放大小]");  
  8.         $(‘#chart_img‘)[0].style.removeExpression(‘width‘); /*ie6补丁*/  
  9.     }  
  10.       
  11. }  

  

这个事例只有在IE6中才能体现removeExpression()的作用,ie7以上是不需要的。

去掉CSS中的表达式Expression

标签:style   http   io   os   ar   使用   java   strong   sp   

原文地址:http://www.cnblogs.com/xiaochao12345/p/4039431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!