jquery点击事件未触发在Internet Explorer上 [英] jquery click event not fired on internet explorer

查看:220
本文介绍了jquery点击事件未触发在Internet Explorer上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击标签时执行某些操作。

 < label class =my-label ; .... 

我想获得点击事件触发
chrome和safari,但不能在IE 11上工作。

  $(。my-label 
});

我尝试了,绑定,生活,但没有成功。



请帮助我。
这里花了2天,在这里疯狂。



谢谢。

解决方案

replace

  $(。my-label)。 $ c> 

  $ my-label)。click(); 

您不是使用 >。你只是引用它。我不知道为什么它在Safari / Chrome中工作:它不应该。






编辑:用户编辑了问题,上下文。



最有可能的是,它现在不工作的原因是因为你缺少一个文件准备好了。用您的代码替换您的代码:

  $(document).ready(function(){

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

});

请回报您在JavaScript控制台中看到的内容。



如果现在它工作,Safari / Chrome已经确定的原因是因为在那些浏览器中,代码是在DOM渲染后执行的。这只是运气。从来没有你应该依赖于DOM准备:总是把所有的代码引用任何DOM节点在 $(document).ready()调用



如果这在IE11仍然不工作,那么你应该为你的问题提供更多的上下文。例如, console.log($(。my-label)。length)会返回什么?如果它是 0 ,则IE11不会找到该节点。同样,发生这种情况的唯一原因是因为DOM没有准备好,或者代码本身没有被调用。如果在 $(。my-label)之前有任何错误,请单击()该代码将不会执行。请注意,在正常浏览器中接受的许多错误不在IE中。如果你不发布你在IE11的JavaScript控制台看到,我认为它是清楚的(因此没有错误),但我不知道你是否检查,所以我把这个可能性。 p>

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.

解决方案

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.

这篇关于jquery点击事件未触发在Internet Explorer上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆