2011-11-29 40 views
3
双空格最好的解决方法

假设你有代码:什么是jQuery.html()取消对IE7和IE8

<!DOCTYPE html> 
<html> 
<head> 
    <title>jQuery.html() - bug on IE7 and IE8</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> 
    <script> 
     $(function() { 
      $("#list").html('<li>List item with double spaces.</li>'); 
      console.log("Does inner html contain double space? ", $("#list").html().indexOf(" ") != -1); 
      console.log("Binded HTML:", $("#list").html()); 
     }); 
    </script> 
</head> 
<body> 
    <ul id="list"> 
    </ul> 
</body> 
</html> 

在IE9,FF,Chrome浏览器的罚款(结合HTML双码),但在IE7和IE8上,我将双空间转换为一个空格。比较不同浏览器上的控制台输出。我不能使用jQuery.text(),因为我不得不使用html内容。

你有没有遇到过这样的错误?你使用了哪种解决方法?

+1

使用正则表达式来做到这一点。 – ppumkin

回答

0

即使使用正则表达式(如/\s\s+/g),正如您所说,在IE中双空格使用html()转换为一个空格。

你必须与原来的内容这样的工作:

var content='<li>List item with double spaces.</li>'; 
$("#list").html(content); 
console.log("Does inner html contain double space? ", content.indexOf(" ") != -1);