时光博客 - WEB开发中的可用性和用户体验
Tag: ie

JS innerHTML Double Quotes

通过jsinnerhtml来获取一个dom节点,内部的html值时,如果属性值没有空格,则ie左右版本下,都会出现标签大些,双引号丢失问题。如果属性值有空格,则标签会变成大写,引号则正常输出.看下面示例:

当属性值无空格时:
FF3.5.7(gecko/20091221)<div id="div2"></div>

IE6 弹出:<DIV id=div2></DIV>
IE7 弹出:<DIV id=div2></DIV>
IE8 弹出:<DIV id=div2></DIV>

有空格时:
FF3.5.7(gecko/20091221)<div id="div2 "></div>

IE6 弹出:<DIV id="div2 "></DIV>
IE7 弹出:<DIV id="div2 "></DIV>
IE8 弹出:<DIV id="div2 "></DIV>

产生这个BUG的原因是什么呢?

When you get the innerHTML of a DOM node in IE, if there are no spaces in an attribute value, IE will remove the quotes around it.IE's innerHTML doesn't remove quotes from non standard attributes.

即使是jQuery里面,jquery团队的技术人员也没有解决这个问题.

提供下解决方案:

  1. function ieInnerHTML(obj) { 
  2.      var zz = obj.innerHTML, 
  3.          z =  
  4.        zz.match(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/g); 
  5.       if (z){ 
  6.         for (var i=0;i<z.length;i++){ 
  7.           var y, zSaved = z[i]; 
  8.           z[i] = z[i].replace(/(<?\w+)|(<\/?\w+)\s/, 
  9.                               function(a){return a.toLowerCase();}); 
  10.           y = z[i].match(/\=\w+[?\s+|?>]/g); 
  11.            if (y){ 
  12.             for (var j=0;j<y.length;j++){ 
  13.               z[i] = z[i].replace(y[j],y[j] 
  14.                          .replace(/\=(\w+)([?\s+|?>])/g,'="$1"$2')); 
  15.             } 
  16.            } 
  17.            zz = zz.replace(zSaved,z[i]); 
  18.          } 
  19.        } 
  20.       return zz; 
  21.      } 

在相应的地方调用函数ieInnerHTML

  1. if(/*@cc_on!@*/0){ 
  2.     inner_str = ieInnerHTML(obj); 
  3. }else
  4.     inner_str = obj.innerHTML; 

 原理:对IE版本进行对应的JS处理,而且相应的元素标签中不要出现非标准元素

参考资料:

  1. innerHTML removes attribute quotes in Internet Explorer
  2. innerHTML中的双引号哪去了?

 

IE6中的min-width一般解决方法

上次翻译的一篇文章How to Use CSS to Solve min-width Problems in Internet Explorer中已经找到了非JavaScript和css expression 实现ie中的min-width

这次我们看看CSS expression的实现方法:

  1. #content{   
  2. _width: expression(((document.compatMode && document.compatMode=='CSS1Compat')? document.documentElement.clientWidth : document.body.clientWidth) < 700'600px' : 'auto');   
  3. }  

这里的 

  1. ((document.compatMode && document.compatMode=='CSS1Compat')? document.documentElement.clientWidth : document.body.clientWidth) < 700  

只是个判定条件,判定页面body的宽度,如果页面body的宽度小于700(这个要根据具体的页面来指定),就把content的宽度指定为600,否则就让它自适应宽度。这里的content宽度,可以根据自己的情况来设定。

[转载]注释在IE中造成文字溢出的研究

在蓝色论坛看到这样一篇帖子 ie中发现新bug重影”》,觉得很有意思。于是在想到底是什么原因造成的呢?便做了以下测试:

实验代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>多了一只猪</title>
</head>
<body>
<div style="width:400px">
<div style="float:left"></div>
<!– –>
<div style="float:right;width:400px">↓这就是多出来的那只猪</div>
</div>
</body>
</html>