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>
<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的一些问题”。

posted on   orc  阅读(3180)  评论(0编辑  收藏  举报

编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具

导航

< 2008年6月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12

统计

点击右上角即可分享
微信分享提示