[英]How do I load a Javascript which contains document.write multiple times in a webpage?
I have a javascript code presented in the head tag:我在 head 标签中有一个 javascript 代码:
document.write('<script>..</script><style>..</style><a></a><script src".."></script>');
The code inside the document.write()
is actually an html document of a sample webpage with scripts
, styles
, metas
and all other possible elements. document.write()
中的代码实际上是一个示例网页的metas
文档,其中包含scripts
、 styles
、元数据和所有其他可能的元素。
Now i need to run this javascript code each time in any element where sample-js-doc
attribute tag is presented in the body
.现在我需要每次在body
中出现sample-js-doc
属性标签的任何元素中运行这个 javascript 代码。 For example: <div sample-js-doc ></div>
or <a sample-js-doc ></a>
例如: <div sample-js-doc ></div>
或<a sample-js-doc ></a>
Currently, where-ever I place the javascrit, the content loads there quickly.目前,无论我将 javascrit 放在哪里,内容都会快速加载到那里。 That's look good.那看起来不错。 But I need it also to be invoked in other places where sample-js-doc
attribute tag is present.但我还需要在存在sample-js-doc
属性标签的其他地方调用它。
How do I modify the javascript code to achieve this without removing all other elements present in the webpage?如何修改 javascript 代码以实现此目的而不删除网页中存在的所有其他元素?
Use querySelectorAll
使用querySelectorAll
var els = document.querySelectorAll('[sample-js-doc]');
for (i = 0; i < els.length; i++) {
els[i].innerHTML = '<script>..</script><style>..</style><a></a><script src".."></script>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.