Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

javascript - jquery click event not fired on internet explorer

I am trying to do something when user clicks on label.

<label class="my-label"> ....

I am trying to get the click event fired It is working on chrome and safari but not working on IE 11.

$(".my-label").click(function(){
});

I tried on, bind, live, but no success.

Please help me with this. Spent 2 days for this, going crazy here.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

replace

$(".my-label").click

with

$(".my-label").click();

You're not calling the method with click. You're just referencing it. I wonder why it works in Safari/Chrome: it shouldn't.


Edit: user edited the question, providing more context.

Most likely, the reason why it is not working now is because you're missing a document ready. Replace your code with:

$(document).ready(function(){ 

    console.log("document ready!");
    $(".my-label").click(function(){
        console.log("click function!");
    });

});

Please report back with what you see in your JavaScript console.

If now it is working, the reason why Safari/Chrome were already ok is because, in those browser, the code was executed after the DOM rendering. That was just luck. Never you should rely on the DOM being ready: always put all of the code referencing any DOM node inside of a $(document).ready() call.

If this is still not working in IE11, then you should provide more context to your question. For example, what does console.log($(".my-label").length) returns? If it is 0, then IE11 is not finding the node. Again, the only reason why this should happen is because the DOM is not ready or, perhaps, the code itself doesn't get called. If you have any error before $(".my-label").click() that code won't get executed. Please note that many "errors" that are accepted in normal browser are not in IE. If you don't post what you see in IE11 JavaScript console, I assume it is clear (and thus there are no errors) but I don't know if you even checked that so I'm taking this as a possibility.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...