我没有尝试对内容做任何事情(至少现在还没有。)我正在尝试做的是搜索特定的标签,然后用另一个标签替换它们。
我得到的HTML充满了具有相同ID的这些标签(我知道这太可怕了!)
<div dir="rtl" id="book-container">
some text here #1
</div>
<div dir="rtl" id="book-container">
some text here #2
</div>
<div dir="rtl" id="book-container">
some text here #3
</div>
.
.
.
<div dir="rtl" id="book-container">
some text here #49
</div>
<div dir="rtl" id="book-container">
some text here #50
</div>
我正在尝试编写一个函数,该函数将搜索每个函数<div dir="rtl" id="book-container">
并将其删除而不删除内部文本。然后搜索所有内容</div>
并将其删除。然后,将整个包装在<p>
标签中。
结果应该是这样的:
<p>
some text here #1
some text here #2
some text here #3
.
.
.
some text here #49
some text here #50
</p>
这是我对问题的解决方法,并附有解释每个步骤的注释。
我对三个元素进行了采样,并将它们包含在body
标签中:
/* Create a paragraph element where the content will be displayed */
const theParagraph = document.createElement("p");
/* Choosing the elements */
const soWrongNodes = document.querySelectorAll("#book-container")
/* For each element, put its content in the paragraph then remove the
element */
soWrongNodes.forEach(soWrongNode => {
theParagraph.innerHTML += soWrongNode.innerHTML;
theParagraph.innerHTML += "<br>";
soWrongNode.remove();
})
/* Append the paragraph element to the body */
document.body.appendChild(theParagraph);
<body>
<div dir="rtl" id="book-container">
some text here #1
</div>
<div dir="rtl" id="book-container">
some text here #2
</div>
<div dir="rtl" id="book-container">
some text here #3
</div>
</body>
这是一个fiddle.js,您可以在其中查看结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句