1.一个很小的问题2.代码:对于innerText显示的是文本内容,为什么去更改这个文本内容的时候却是把这个标签组也给删除了?有句话是:node.innerTextSets or gets the text between the start and end tags of the object另外,有没有其他方法(jquery也可)可以只替换文本内容而不把i标签也删了?
1 回答

富国沪深
TA贡献1790条经验 获得超9个赞
找childNodes把,判断是文本节点看里面有没有类容,有就干掉
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p onclick="aaa(this)">
<i>1111</i>
aaaaaaa
</p>
<script>
function aaa(obj){
for(el of obj.childNodes){
if(el.nodeType==3&&!!el.nodeValue.replace(/\s+/g,'')){
console.log(el)
el.nodeValue = ''
}
}
}
</script>
</body>
</html>
添加回答
举报
0/150
提交
取消