且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

强制脚本在AJAX加载页面中运行

更新时间:2023-12-05 09:57:34

在进行任何其他操作之前,应将函数包装在.ready()中,以确保在对这些元素执行某些操作之前,已加载所有DOM元素

Before anything else, you should wrap your function in a .ready() to ensure all DOM elements are loaded before you do something to those elements

此外,仅当HTML附加在DOM中时,才会执行通过jQuery中的ajax函数返回的HTML中的脚本.由于您未附加包含代码的html,因此返回的内容中的脚本将无法运行.

Also, scripts within HTML that are returned via ajax functions in jQuery are executed only when the HTML is appended in the DOM. Since you did not append the html containing the code, the script in that returned content won't run.

dataType .ajax() 函数的参数/p>

Take a look at the .ajax() function's parameters, under dataType

数据类型:

...

" html&quot ;:以纯文本格式返回HTML;否则,返回HTML.当插入DOM中时,评估包含的脚本标签.

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

...