gpt4 book ai didi

javascript - jQuery:在脚本插入时调用 globalEval

转载 作者:行者123 更新时间:2023-11-30 17:17:33 24 4
gpt4 key购买 nike

我使用 jQuery("body").append(elementBelow) 将这段 html 插入到 dom 中:

<div>...</div>
<script type="...">
// and this is being executed through jQuery.globalEval()
</script>

插入 dom 时,为什么浏览器本身不按应有的方式处理执行?为什么 jQuery 会执行这段代码?

如果 jQuery 正在执行这个,为什么这个代码在添加到 dom 时也没有执行?间接方法 eval 是在 globalEvent 方法中使用的方法:

// Evaluates a script in a global context
globalEval: function( code ) {
var script,
indirect = eval;

code = jQuery.trim( code );

if ( code ) {
// If the code includes a valid, prologue position
// strict mode pragma, execute code by injecting a
// script tag into the document.
if ( code.indexOf("use strict") === 1 ) {
script = document.createElement("script");
script.text = code;
document.head.appendChild( script ).parentNode.removeChild( script );
} else {
// Otherwise, avoid the DOM node creation, insertion
// and removal by using an indirect global eval

// !!!!!!!!!!! ENDING UP HERE HERE!!!!!!!!
indirect( code );
}
}
},

我只是想了解这一切是如何工作的,因为它正在被评估并插入到 dom 中。

最佳答案

Why aren't the browser itself handling the executing as it should, when inserted into the dom? Why is jQuery executing this piece of code?

因为当您通过 innerHTML 插入 DOM 时,浏览器不会执行 script 元素。因此,由于这通常是人们真正想要的,jQuery 会为您完成。

关于javascript - jQuery:在脚本插入时调用 globalEval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25812505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com