jQuery 对象不支持此属性或方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5940028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:03:20  来源:igfitidea点击:

Object doesn't support this property or method

jquerywordpresse-commerce

提问by Jone

As titled, I'm getting this error on my site. I have checked through the IE8 developer debugging tool, and I have got the following code that caused the error.

如标题所示,我在我的网站上收到此错误。我已经通过IE8开发人员调试工具检查过,我得到了以下导致错误的代码。

<!-- slider js code start -->
<script type="text/javascript">
$().ready(function() {
    if(eval(document.getElementById('coda-slider-1')))
    {
        $('#coda-slider-1').codaSlider();
        //jQuery.noConflict(); var $j = jQuery;
    }
}); 

I have included the screenshoot from Chrome debugging tool.

我已经包含了 Chrome 调试工具的屏幕截图。

http://img857.imageshack.us/i/javaerror.jpg

http://img857.imageshack.us/i/javaerror.jpg

http://img204.imageshack.us/i/javaerror2.jpg

http://img204.imageshack.us/i/javaerror2.jpg

Please help me to figure it out.

请帮我弄清楚。

Thank you.

谢谢你。

回答by Ender

Try this instead:

试试这个:

$(function() {
    if($('#coda-slider-1').size())
    {
        $('#coda-slider-1').codaSlider();
        //jQuery.noConflict(); var $j = jQuery;
    }
}); 

Your original code says "select nothing with jQuery, and apply this ready handler to it." The correct long-hand syntax would be:

您的原始代码表示“不使用 jQuery 选择任何内容,并将此就绪处理程序应用于它”。正确的长手语法是:

$(document).ready(function() { ...

Also note that I have removed evalbecause it should never, ever be used, unless it can't possibly be avoided.

另请注意,我已将其删除eval是因为它永远不应该被使用,除非它不可能避免。

UPDATE

更新

Looking at your error screenshots, it appears that jQuery is not defined (at least not with the $alias. Have you included the script on your page? If so, are you calling jQuery.noConflict()before your ready handler is bound?

查看您的错误屏幕截图,似乎未定义 jQuery(至少没有定义$别名。您是否在页面上包含了脚本?如果是这样,您是否jQuery.noConflict()在绑定就绪处理程序之前调用?

Try putting this script tag above both the code you posted, and the script tag for the coda slider:

尝试将此脚本标签放在您发布的代码和尾声滑块的脚本标签之上:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>

UPDATE 2

更新 2

As pointed out by kingjiv in the comments below, I was mistaken, and $().readywill work (though it is not recommended). I believe my first update, noting that jQuery appears not to be defined, is the actual issue here.

正如kingjiv在下面的评论中指出的那样,我错了,并且$().ready会起作用(尽管不推荐)。我相信我的第一次更新,注意到 jQuery 似乎没有被定义,是这里的实际问题。

回答by James Montagne

I'm guessing you're trying to check for the existence of coda-slider-1?

我猜你是想检查coda-slider-1?

No need to use eval, and if you're using jquery, may as well select the element with jquery:

不需要使用eval,如果你使用jquery,不妨用jquery选择元素:

if($("#coda-slider-1").length>0){

}

回答by Erick Petrucelli

Problems:

问题:

  • $().readyisn't recommended. Use $(document).ready(functionor simple $(function.

  • Why using document.getElementByIdif jQuery already lookup elements using selectors in a crossbrowser way? Just do $("#some").lengthto see if it exists.

  • Also in your case I think is good to ensure that the codaSlider()method is loaded before calling.

  • $().ready不推荐。使用 $(document).ready(function或简单$(function

  • document.getElementById如果 jQuery 已经以跨浏览器的方式使用选择器查找元素,为什么要使用它?只是$("#some").length看看它是否存在。

  • 同样在您的情况下,我认为确保codaSlider()在调用之前加载该方法是很好的。

Correted code:

相关代码:

$(function() {
    if ($("#coda-slider-1").length > 0 && $("#coda-slider-1").codaSlider) {
        $('#coda-slider-1').codaSlider();
    }
});