gpt4 book ai didi

jQuery 内容不是函数

转载 作者:行者123 更新时间:2023-11-27 23:26:50 25 4
gpt4 key购买 nike

此 jQuery 代码试图获取页面中的所有评论元素,但在浏览器控制台中运行时返回错误。

TypeError: $(...).contents is not a function

如何获取页面中所有的评论节点?谢谢

$("*").contents().filter(function() {
return this.nodeType == 8;
})

最佳答案

我认为 $ 引用了您页面中的一些其他库,因此请使用 jQuery 而不是 $。还要检查 jQuery 库是否包含在页面代码之前。

jQuery("*").contents().filter(function() {
return this.nodeType == 8;
})

或者使用闭包将$作为函数内部的jQuery

(function($){
$("*").contents().filter(function() {
return this.nodeType == 8;
})
})(jQuery);

jQuery(function($){
$("*").contents().filter(function() {
return this.nodeType == 8;
})
})

在这里阅读更多:Avoiding Conflicts with Other Libraries


也不要忘记用 document ready handler 包装代码.

关于jQuery 内容不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38156953/

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