简体   繁体   English

jQuery .click()不会触发

[英]jQuery .click() doesn't fire

I'm having a secluded problem. 我有一个僻静的问题。 I was creating a UI for something using jQuery when all of a sudden the .click() events stopped firing. 突然.click()事件停止触发时,我正在使用jQuery创建某些内容的UI。

This jsFiddle has all of the related code: 该jsFiddle具有所有相关代码:

http://jsfiddle.net/qgz2j/1/ http://jsfiddle.net/qgz2j/1/

If you look at the code, you can see that a <div> with id "target" contains two other <div> s, one with id "red" and one with id "green". 如果你看一下代码,你可以看到一个<div> ID为“目标”包含两个其它<div> S,一个ID为“红色”和一个ID为“绿色”。 Looking at the associated CSS, both have width 100% and are overlapped, but the green has display:none; 查看关联的CSS,两者的宽度均为100%,并且重叠,但是绿色的display:none; .

As you can see, the .click() doesn't fire and no alert is shown. 如您所见, .click()不会触发,也不会显示警报。 However, using this works: 但是,使用此功能:

<div id="target" onClick="alert('Clicked!')">
    <div id="red"></div>
    <div id="green"></div>
</div>

Thanks for the help! 谢谢您的帮助!

You chose a wrong framework. 您选择了错误的框架。 Change it From Mootools to jQuery . 将其从Mootools更改为jQuery

在此处输入图片说明

Also It is a good practice to wrap your code inside the ready function. 另外,将代码包装在ready函数中也是一种好习惯。

$(function(){     
       $('div#target').click(function() {
            alert('div has been clicked');
        });

});
​ 

Working sample http://jsfiddle.net/qgz2j/4/ 工作样本http://jsfiddle.net/qgz2j/4/

现在检查一下,您不附加jQuery库http://jsfiddle.net/qgz2j/3/

In the fiddle, Choose framework as jQuery and it should work. 在小提琴中,选择jQuery框架,它应该可以工作。 :P :P

http://jsfiddle.net/qgz2j/6/ http://jsfiddle.net/qgz2j/6/

Your code is fine you just had your fiddle set to use MooTools instead of jQuery. 您的代码很好,您只是设置了小提琴即可使用MooTools而不是jQuery。 Take a look at this version: 看一下这个版本:

http://jsfiddle.net/qgz2j/8/ http://jsfiddle.net/qgz2j/8/

All I did was tell it to use jQuery. 我所做的只是告诉它使用jQuery。

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

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