我正在编写一个 AJAX 站点,为了执行我导入的脚本,我必须创建新的相同脚本以便 DOM 运行它们。我想知道是否有一种简单的内置方法或另一种简单的方法可以将一个脚本标签的所有属性复制到另一个脚本标签,而无需单独传输它们。
基本上我正在寻找的是:
在 HTML 中:
<script id = "myscript" src = "somefile.js" type = "text/javascript"></script>
在 JS 中:
var script = document.createElement("script");
script.innerHTML = document.getElementById("myscript").innerHTML;
script.attributes = document.getElementById("myscript").attributesList;
我目前的做法是将每个属性单独分配给新的脚本对象,我认为这有点乏味。
最佳答案
我最终决定只使用一个循环来填充新脚本元素上的属性列表:
for (index = old_script.attributes.length - 1; index > -1; -- index) {
attribute = old_script.attributes[index];
new_script.setAttribute(attribute.name, attribute.value);
}
关于javascript - 有没有办法将所有属性从一个元素转移到另一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43551594/