在正文中搜索<div> <br></div>
的所有实例,并使用JavaScript
答案 0 :(得分:2)
只需在.textContent
元素上使用 String.replace()
方法。
您是否应该完全清楚整个div
是否应该被替换,或者只是替换它内部的
。以下是处理两者的示例:
只替换
:
// Get all the elements that need work into an array
let elements = Array.prototype.slice.call(document.querySelectorAll("div"));
// Loop over the array and replace the HTML entity with a space char.
elements.forEach((el) => { el.textContent = el.textContent.replace(" ", " "); });
&#13;
<div> <br></div>
<div> <br></div>
<div> <br></div>
<div> <br></div>
&#13;
替换整个div
:
let el = document.getElementById("elementToSearch");
// Just replace all occurences of the element with a space
el.innerHTML = el.textContent.replace("<div> <br></div>", " ");
&#13;
/* You won't see this style applied anywhere because
all the div elements have been removed*/
#elementToSearch div { width:400px; height:100px; background-color:yellow; }
&#13;
<div id="elementToSearch">
<div> <br></div>
<div> <br></div>
<div> <br></div>
<div> <br></div>
</div>
&#13;
答案 1 :(得分:0)
此示例使用直接Javascript。
myDivList = document.getElementsByTagName("div");
for (var i = 0; i < myDivList.length; i++) {
if (myDivList[i].innerHTML == " <br>") {
myDivList[i].innerHTML = "New value.";
}
}
&#13;
<div> <br></div>
<div>Not this one.</div>
<div>Not this one.</div>
<div> <br></div>
&#13;