手掌心
2019-02-19 12:19:35
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
提交
取消