简体   繁体   English

jQuery单击事件在页面上不起作用,但它们适用于jsFiddle

[英]jQuery click events don't work on page, but they work on jsFiddle

I checked js tags are just fine, everything is closed, but the js all of sudden decides not to work. 我检查了js标签就好了,一切都关闭了,但js突然决定不起作用。 I have the code working in JSFiddle so I know it has to be something I did or possibly the server. 我有代码在JSFiddle工作,所以我知道它必须是我做的或可能是服务器。 I just need some outside influence to make sure it's not oversight. 我只需要一些外部影响力,以确保它不是疏忽。

This is the site with the problem. 这是有问题的网站 Here's the code: 这是代码:

$(document).ready(function(){
    $(".designers").show();
    $('.list').hide();
});

$(".but").click(function(){
    $(".designers").hide();
    $(".list").show("slide", { direction: "down" }, 1000);
});

$(".but2").click(function(){
    $(".designers").show();
    $(".list").hide("slide", { direction: "down" }, 1000);
});

The event handlers should go in the ready function as well: 事件处理程序也应该进入就绪函数:

$(document).ready(function(){
    $(".designers").show();
    $('.list').hide();

    $(".but").click(function(){
        $(".designers").hide();
        $(".list").show("slide", { direction: "down" }, 1000);
    });

    $(".but2").click(function(){
        $(".designers").show();
        $(".list").hide("slide", { direction: "down" }, 1000);
    });
});

JsFiddle automagically sticks everything in a document ready, that's why it works there and not on your site, as you're trying to fetch the elements before they are added to the DOM. JsFiddle自动将所有内容都放在文档中,这就是为什么它在那里工作而不是在你的网站上工作,因为你试图在将元素添加到DOM之前获取它们。

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

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