下面是许多用户脚本中使用的代码:
function with_jquery(f) {
var script = document.createElement("script");
script.type = "text/javascript";
script.textContent = "(" + f.toString() + ")(jQuery)";
document.body.appendChild(script);
};
除了这一行,我理解了其中的所有内容:
script.textContent = "(" + f.toString() + ")(jQuery)";
我知道文本内容设置脚本的文本内容(当然),但我无法理解 =
之后的所有内容。这不应该是:
script.textContent = string;
最佳答案
通过用 (
f.toString() )(jQuery)
包装字符串,用户可以将要执行的字符串设置为 immediately invoked function expression ,将 jQuery 对象传递给该函数。
那么,我们希望 f.toString()
看起来像
function($){ [doing something...] }
这样 jQuery 对象将代表 $
当然,新字符串仍然需要评估。
关于javascript - 关于(jquery)的解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21796272/