innerHTML未知的运行错误
代码:
<!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>
<title>innerHTML未知的运行错误</title>
</head>
<body>
<p id="p"></p>
<script type="text/javascript">
var p=document.getElementById('p');
p.innerHTML='<div>innerHTML简单易用,不过</div>';
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>innerHTML未知的运行错误</title>
</head>
<body>
<p id="p"></p>
<script type="text/javascript">
var p=document.getElementById('p');
p.innerHTML='<div>innerHTML简单易用,不过</div>';
</script>
</body>
</html>
运行以上代码,IE里提示:未知的运行错误。FF测试是正确的。
Goole一下,innerHTML 对下面的对象只读(<=IE7):table,thead,tfoot,tbody,tr,col,colgroup,html,title,style,frameset等。
上面所说的innerHTML属性只读的对象中并不包括p,为什么还报错。
再google,找到这么一段:
The<p>
tag can be nested within certain tags and certain tags can be nested within it. The general rule is that the paragraph has to be its own block. Therefore, a paragraph tag cannot be nested within a tag expecting only text or inline elements, nor can another block element tag be nested within a paragraph tag.
For instance, you cannot nest paragraph tags and heading tags.
You can also put paragraph tags within division tags but not the other way around.
A paragraph tag can be placed within a table cell or other tags structures that are not text only. It can also be placed within a list item. This means you can have long, multi-paragraph list items. Anything can be nested within a<p>
tag that is allowed within the flow of the text. All style tags are allowed within a paragraph since they format text. Links and images can be placed within paragraphs, and the<br />
tag exists for the exclusive purpose of breaking the line without ending the paragraph.
If a Web browser encounters a tag within a paragraph that cannot be nested within paragraph tags, it will assume that the paragraph has ended.
大家自己看吧, 其中的大意之一:p标签不能包容div、h1除了<br />等块级元素。
所以IE报未知的运行错误原因就是innerHTML里有div标签,另外往p元素里插入div元素也会失败 【p.appendChild(div)】
最后,蓝色理想上的一篇blank的“innerHTML的一些问题”。