本文介绍了如何使用Javascript替代DOM元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找替换DOM中的一个元素。例如,我想用SPAN替换一个A元素。我该怎么去做?
解决方案
使用:
< html>
< head>
< / head>
< body>
< div>
< a id =myAnchorhref =http://www.stackoverflow.com> StackOverflow< / a>
< / div>
< script type =text / JavaScript>
var myAnchor = document.getElementById(myAnchor);
var mySpan = document.createElement(span);
mySpan.innerHTML =replace anchor!;
myAnchor.parentNode.replaceChild(mySpan,myAnchor);
< / script>
< / body>
< / html>
I am looking to replace an element in the DOM. For example, there is an "A" element that I want to replace with a "SPAN" instead.
How would I go and do that?
解决方案
by using 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>
这篇关于如何使用Javascript替代DOM元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!