当使用类选择器时,jquery点击事件被触发多次 [英] jquery click event is firing multiple times when using class selector

查看:586
本文介绍了当使用类选择器时,jquery点击事件被触发多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的html ;
< li>< div class =myLinkid = 2> b< div>
< li>< div class =myLinkid = 3> c< div>
< li>< div class =myLinkid = 4> d< div>
< li>< div class =myLinkid = 5> d< div>
< li>< div class =myLinkid = 6> e< div>
< li>< div class =myLinkid = 7> d< div>
< li>< div class =myLinkid = 8> g< div>

我在此代码中创建了一个jquery事件绑定:

  jQuery(。myLink)。click(function(event){

var myId = this.id;

location.href ='/ x / y?myId ='+ myID;
});当我点击其中一个链接(li项目)时,

。我认为会激发一个点击事件,当我调用this.id,我只会得到我点击的项目的id。



但它看起来像:

  jQuery(。myLink)click(function(event){

即使我只是点击了一个链接,我发出了一个调试器语句并使用firebug,看到这个被调用



任何想法怎么回事?

解决方案

你关闭你的< li> 标签,你会解决这个问题我刚刚测试了它,并更正它
应该

 < li> ...< / li> 
< li> ...< / li>

 < li> 
< li>


here is my html

<li><div class="myLink" id=1>A<div>
<li><div class="myLink" id=2>b<div>
<li><div class="myLink" id=3>c<div>
<li><div class="myLink" id=4>d<div>
<li><div class="myLink" id=5>d<div>
<li><div class="myLink" id=6>e<div>
<li><div class="myLink" id=7>d<div>
<li><div class="myLink" id=8>g<div>

i created a jquery event bind wiht this code:

    jQuery(".myLink").click(function(event) {

         var myId = this.id;

         location.href = '/x/y?myId=' + myID;
   });

when i click on one of the links (the li items). i thought that would fire one click event and when i call this.id, i would just get that id of the item that i clicked.

But instead it looks like the:

   jQuery(".myLink").click(function(event) {

is firing over and over again even thought i just clicked on one link. I put a debugger statement in their and used firebug and saw this getting called over and over.

Any ideas whats going on?

解决方案

If you close your <li> tags, you'll fix the problem. I've just tested it, and that corrects it. Should be

<li> ... </li>
<li> ... </li>

not

<li>
<li>

这篇关于当使用类选择器时,jquery点击事件被触发多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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