View Source
Copy Code code as follows:
return this[0] && This[0].nodetype = = 1?
This[0].innerhtml.replace (Rinlinejquery, ""):
Null
Know that it passes through non-standard innerHTML implementations that are supported by all browsers.
Some consumers use the return value of the HTML () method as a condition for code branching, such as:
Copy Code code as follows:
var str = $ (' #user '). html ();
if (str== ' Jack ') {
...
}else if (str== ' Tom ') {
...
}else if (str== ' lily ') {
...
}
In most cases this is fine, but if there are spaces inside the HTML element of the Id=user, you won't get the results you want. Like what:
Copy Code code as follows:
<div id= "User" > Jack</div>
<script>
Alert (document.getElementById (' user '). innerhtml.length);
</script>
Div[id=user] inside the text jack before the more than careful 3 spaces, this time in the browser performance is not the same:
IE6/78, the length of the pop-up string is 4, that is, the space is ignored.
In Ie9/firefox/safari/chrome/opera, the pop-up is 7, that is, no spaces are ignored.
The condition that takes the return value of the. html () as a branch of code is clearly an error in non IE browsers.
The solution is simple if you want to use the HTML content of the element as a criterion
1, when writing HTML, remove space
2, call the HTML () method and then call trim, such as var str = $ (' #user '). html (). Trim ();
Related:
Http://www.w3.org/TR/2008/WD-html5-20080610/dom.html
Only innerHTML return values in IE6/7/8 ignore English spaces