一个关于innerText和innerHTML的小疑问

1.一个很小的问题

2.代码:

对于

https://img4.mukewang.com/5c6faf0f0001e44005600274.jpg


innerText显示的是文本内容,为什么去更改这个文本内容的时候却是把这个标签组也给删除了?

有句话是:

node.innerText

Sets or gets the text between the start and end tags of the object

另外,有没有其他方法(jquery也可)可以只替换文本内容而不把i标签也删了?


手掌心
浏览 447回答 1
1回答

富国沪深

找childNodes把,判断是文本节点看里面有没有类容,有就干掉<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <title>Document</title></head><body>&nbsp; &nbsp; <p onclick="aaa(this)">&nbsp; &nbsp; &nbsp; &nbsp; <i>1111</i>&nbsp; &nbsp; &nbsp; &nbsp; aaaaaaa&nbsp; &nbsp; </p>&nbsp; &nbsp; <script>function aaa(obj){&nbsp; &nbsp; for(el of obj.childNodes){&nbsp; &nbsp; &nbsp; &nbsp; if(el.nodeType==3&&!!el.nodeValue.replace(/\s+/g,'')){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(el)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; el.nodeValue = ''&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}&nbsp; &nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript