我有一个如下所示的节点
<body id="hello" dir="auto" style="margin-top:0;margin-bottom:0;color:red" bgcolor="green";></body>
我要提取id="hello" dir="auto" style="margin-top:0;margin-bottom:0;color:red" bgcolor="green";
从 body 标签并将其应用到另一个标签(例如 DIV)。这怎么可能。
最佳答案
只需做这样的事情,不需要 jQuery。
var newDiv = document.body.cloneNode();
document.body.appendChild(newDiv);
newDiv.outerHTML = newDiv.outerHTML.replace(/body/g,"div");
fiddle
或者甚至是更干净的方式 seen here
var newDiv = document.createElement('div'),
body = document.body;
for (var i = body.attributes.length; i--;) {
var attr = body.attributes.item(i);
newDiv.setAttribute(attr.nodeName, attr.nodeValue);
}
它将元素(在本例中为主体)中的所有属性复制到新的 div。
关于javascript - 如何在 JavaScript 中获取节点数据,例如内联样式和方向等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24796885/