简体   繁体   English

如何运行<script> when innerHTML is changed

[英]How to run a <script> when innerHTML is changed

I have a <script> tag, and I want the script tag to run whenever the innerHTML of that script tag is changed.我有一个<script>标签,我希望脚本标签在该脚本标签的innerHTML发生更改时运行。 I don't care if the solution is in HTML or JS or JQuery.我不在乎解决方案是在 HTML 还是 JS 或 JQuery 中。

Here is my code (With JQuery 3.4.1):这是我的代码(使用 JQuery 3.4.1):

<script id="sc"></script>

<script>
var sc = $('#sc');

function runScript () {
  sc.html('console.log("Hello World");');
}
runScript();

I think the reason is because the script tag is run once at the beginning, and then never again, but I could be mistaken.我认为原因是因为脚本标签在开始时运行一次,然后再也不会运行,但我可能会误会。 Any help is greatly appreciated.任何帮助是极大的赞赏。

Thank you.谢谢你。

This is impossible and your syntax is wrong.这是不可能的,而且您的语法是错误的。 you need to learn some knowledge about the rendering of the web and how the web interacts with javascript.你需要学习一些关于 web 渲染以及 web 如何与 javascript 交互的知识。 visit https://developers.google.com/web/fundamentals/performance/critical-rendering-path/adding-interactivity-with-javascript访问https://developers.google.com/web/fundamentals/performance/critical-rendering-path/adding-interactivity-with-javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM