测试HTML:

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 
 3 <head>
 4 
 5 </head>
 6 
 7 <body ><p id="msgp">
 8 
 9 <div>
10 
11 <span id="msg">
12 
13 xxxxxxooooo</span></div></p>
14 
15 </body>
16 
17 </html>

问题症状:通过document.getElementByID("msg").innerHTML 无法获取span标签的内容,获取值的永远都是空。

解决思路:通过对出现问题的页面进行dom删除,剩下最终出问题的部分。最终确定是由于没有遵循HTML规范在P标记中包含div导致页面渲染成另外一种形式。复制出Chrome开发工具生成的代码如下 

1 <html xmlns="http://www.w3.org/1999/xhtml"><head>
2 </head>
3 <body><p id="msgp">
4 </p><div>
5 <span id="msg">
6 xxxxxxooooo</span></div><p></p>
7 
8 </body></html>

最后把p标签修改成div标签解决了问题。

 

由于没写过文章,说有只是把问题记录下来。方便遇到类似问题的人能搜索到,不必花较多时间去查找原因。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2021-06-14
  • 2021-11-29
  • 2022-12-23
  • 2021-11-06
猜你喜欢
  • 2021-10-02
  • 2021-06-11
  • 2022-12-23
  • 2022-01-24
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案