我想要替换DOM中的一个元素。
例如,有一个<a>
元素,我想要用<span>
替换它。
我该如何操作呢?
通过使用replaceChild()方法:
<html>
<head>
</head>
<body>
<div>
<a id="myAnchor" href="http://www.stackoverflow.com">StackOverflow</a>
</div>
<script type="text/JavaScript">
var myAnchor = document.getElementById("myAnchor");
var mySpan = document.createElement("span");
mySpan.innerHTML = "replaced anchor!";
myAnchor.parentNode.replaceChild(mySpan, myAnchor);
</script>
</body>
</html>
A.replaceWith(span)
- 无需父元素通用形式:
target.replaceWith(element)
比之前的方法好多了/更加干净。
针对您的使用情况:
A.replaceWith(span)
...
)。示例:
// Initially [child1, target, child3]
target.replaceWith(span, "foo") // [child1, span, "foo", child3]
const list = ["bar", span]
target.replaceWith(...list, "fizz") // [child1, "bar", span, "fizz", child3]
null
目标如果你的目标有可能为空,你可以考虑使用新的?.
可选链运算符。如果目标不存在,什么都不会发生。在此处阅读更多信息。
target?.replaceWith?.(element)
支持的浏览器 - 97% Nov '22
var a = A.parentNode.replaceChild(document.createElement("span"), A);
a是被替换的A元素。
oldElement.replaceNode(newElement)
Element.outerHTML = template;
,不需要父元素。// Get the current element
var currentNode = document.querySelector('#greeting');
// Replace the element
currentNode.outerHTML =
'<div id="salutations">' +
'<h1>Hi, universe!</h1>' +
'<p>The sun is always shining!</p>' +
'</div>';
Node.replaceWith(newNode)
替换一个HTML元素或节点。const links = document.querySelectorAll('a')
links.forEach(link => {
const replacement = document.createElement('span')
// copy attributes
for (let i = 0; i < link.attributes.length; i++) {
const attr = link.attributes[i]
replacement.setAttribute(attr.name, attr.value)
}
// copy content
replacement.innerHTML = link.innerHTML
// or you can use appendChild instead
// link.childNodes.forEach(node => replacement.appendChild(node))
link.replaceWith(replacement)
})
如果你有以下这些元素:
<a href="#link-1">Link 1</a>
<a href="#link-2">Link 2</a>
<a href="#link-3">Link 3</a>
<a href="#link-4">Link 4</a>
<span href="#link-1">Link 1</span>
<span href="#link-2">Link 2</span>
<span href="#link-3">Link 3</span>
<span href="#link-4">Link 4</span>
document.querySelectorAll('a')
。 - Nurul Huda我曾经遇到过类似的问题,并在这个讨论中找到了解决方法。使用“替换”操作并没有帮助我解决问题,而根据父元素进行操作又不适合我的情况。使用innerHtml会替换掉子元素,这也不是我想要的结果。最终,使用outerHTML才真正解决了这个问题。希望这个经验能帮助到其他人!
currEl = <div>hello</div>
newElem = <span>Goodbye</span>
currEl.outerHTML = newElem
# currEl = <span>Goodbye</span>
createElement
)后,使用 replaceChild
替换目标元素的父元素:const newElement = document.createElement(/*...*/);
const target = document.getElementById("my-table");
target.parentNode.replaceChild(newElement, target);
insertAdjacentHTML
然后使用removeChild
(或在现代环境中,直接在元素本身上使用remove
):const target = document.getElementById("my-table");
target.insertAdjacentHTML("afterend", theHTMLForTheNewElement);
target.parentNode.removeChild(target); // Or: `target.remove()`
替换LI元素的示例
function (element) {
let li = element.parentElement;
let ul = li.parentNode;
if (li.nextSibling.nodeName === 'LI') {
let li_replaced = ul.replaceChild(li, li.nextSibling);
ul.insertBefore(li_replaced, li);
}
}
鉴于已经提出的选项,最简单的解决方案是不需要查找父级:
var parent = document.createElement("div");
var child = parent.appendChild(document.createElement("a"));
var span = document.createElement("span");
// for IE
if("replaceNode" in child)
child.replaceNode(span);
// for other browsers
if("replaceWith" in child)
child.replaceWith(span);
console.log(parent.outerHTML);
target.replaceWith(element);
是现代(ES5+)实现此操作的方式。 - Gibolt