[英]Removing wrapper div without Jquery (raw javascript)
I know there is a solution with Jquery called unwrap, but I am writing "raw" JavaScript. 我知道有一个解决方案,Jquery称为unwrap,但我正在编写“原始”JavaScript。 I didn't find any solution without jQuery.
没有jQuery我没有找到任何解决方案。
I'd like to remove a div like so: 我想删除这样的div:
<div><div id="mydiv">Important text here</div></div>
After removal of "mydiv": 删除“mydiv”后:
<div>Important text here</div>
What should I do, I'd like to know the theory. 我该怎么办,我想知道这个理论。
Thanks in advance. 提前致谢。
shouldn't this line work 不应该这条线工作
document.getElementById("mydiv").outerHTML = document.getElementById("mydiv").innerHTML
See this JSBin Example (inspect the element) 看到这个JSBin示例 (检查元素)
You need to use removeChild
method for that: 您需要使用
removeChild
方法:
var divToRemove = document.getElementById('mydiv').;
var tmpStr = divToRemove.innerHTML;
var parent = divToRemove.parentNode;
parent.removeChild(divToRemove);
parent.inerHTML(tmpStr);
Try 尝试
var el = document.getElementById('mydiv');
var text = el.innerText; //or el.innerHTML
el.parentNode.innerHTML = text;
This would replace the html of mydiv's parent with the content of mydiv. 这将用mydiv的内容替换mydiv的父级的html。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.