简体   繁体   English

无法调用未定义的方法“单击”

[英]Cannot call method 'click' of undefined

I am trying to implement a jQuery 'slide toggle'. 我正在尝试实现jQuery的“幻灯片切换”。 At the top of the page (in the header) I have included: 在页面顶部(标题中),我包括:

<script src="http://code.jquery.com/jquery-latest.js"></script>

Then the code of the area is: 那么该区域的代码是:

<a href="#" id="morebutton" class="more">More</a>
<div class="toggler">
<div id="morepanel" class="ui-widget-content ui-corner-all">
  <p>Text</p>
    </div>
</div>

<script src="js/toggle.js" type="text/javascript"></script>

The toggle.js contains: toggle.js包含:

$('#morebutton').click(function () {
$('#morepanel').slideToggle('slow', function () {
    // Animation complete.
});
});

When I click it I get the error: 当我单击它时,出现错误:

Toggle.js:1Uncaught TypeError: Cannot call method 'click' of undefined. Toggle.js:1Uncaught TypeError:无法调用未定义的方法“ click”。

I can't at all figure out what to do. 我完全不知道该怎么办。 The divs are nested within further divs, and content holders, etc, but I can't see why this would be an issue. div嵌套在其他div和内容所有者等中,但是我看不到为什么这会成为问题。

You should wrap your code in a $(function() {}); 您应该将代码包装在$(function() {}); statement. 声明。 This will make sure it will execute after the DOM loads . 这将确保它将在DOM加载后执行。

Currently, your code is being executed before the DOM is fully built, resulting in a reference to a DOM-element that does not yet exist. 当前,您的代码是在完全构建DOM之前执行的,从而导致对尚不存在的DOM元素的引用。

Example: 例:

$(function() {
    $('#morebutton').click(function () {
        $('#morepanel').slideToggle('slow', function () {
            // Animation complete.
        });
    });
});

Hotlinking is disabled for files on the jQuery site . jQuery网站上的文件禁用热链接 The file won't load, so jQuery won't be loaded. 该文件不会加载,因此不会加载jQuery。

Use one of the CDNs instead . 改用CDN之一

Eg 例如

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>

jQuery is probably not loaded at this point. 可能此时未加载jQuery。 Wrap your call as such: 这样包装您的电话:

$(document).ready(function() {
  $('#morebutton').click(function () {
    $('#morepanel').slideToggle('slow', function () {
        // Animation complete.
    });
  });
})

It would seem that '#morebutton' isn't actually selecting anything. 似乎“ #morebutton”实际上没有选择任何内容。

Have you tried put a breakpoint on with Firebug or the IE/Chrome equivalents and run the selector from the Console ? 您是否尝试过在Firebug或IE / Chrome等效项上设置断点并从控制台运行选择器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM