我正在编写一个 AJAX 站点,为了执行我导入的脚本,我必须创建新的相同脚本,以便 DOM 运行它们。我想知道是否有一种简单的内置方法或另一种简单的方法可以将一个脚本标记的所有属性复制到另一个脚本标记,而不必单独传输它们。
基本上我要找的是这个:
在 HTML 中:
<script id = "myscript" src = "somefile.js" type = "text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
在JS中:
var script = document.createElement("script");
script.innerHTML = document.getElementById("myscript").innerHTML;
script.attributes = document.getElementById("myscript").attributesList;
Run Code Online (Sandbox Code Playgroud)
我目前的做法是将每个属性单独分配给新的脚本对象,我认为这有点乏味。
我最终决定只用一个循环来填充新脚本元素上的属性列表:
for (index = old_script.attributes.length - 1; index > -1; -- index) {
attribute = old_script.attributes[index];
new_script.setAttribute(attribute.name, attribute.value);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1311 次 |
| 最近记录: |