jQuery点击触发两次,但只注册一次 [英] jQuery click fires twice but only registered once

查看:101
本文介绍了jQuery点击触发两次,但只注册一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元素点击事件,点击两次,一次点击,搜索的常见问题,但我排除了可能的原因从其他答案。代码是

  console.log(注册标签点击); 
$(label.tiletype)。click(
function(event){
event.stopPropagation(); //停止.tile被调用
console.log (label clicked,x =+ event.pageX +,y =+ event.pageY);
//更多代码...
}
}

当页面加载并点击label.tiletype时,日志为

 注册标签click 
标签点击,x = 679,y = 172
标签点击,x = 679,y = 172

由于注册标签点击只发生一次,所以不能因为任何原因而导致点击被绑定两次。



由于pageX和pageY坐标是相同的,所以double事件发生在同一物理点击,而不是元素上调用的第二个程序click()



调试行只在这一段代码中,因此没有其他点击事件触发。



我已经检查了Firebug的源代码,并且有多个label.tiletype元素,但没有重叠在屏幕上,他们都显示:block和float:left。



在JavaScript文件中还有其他事情(第375行 https://github.com/okohll/agileBase/blob/200dd1c6c1a8968bc9d8f5b2b3d9188ac4776984/gtpb_clientside/resources/simple/simple.js ),但无论发生什么,上述语句必须始终为



这是我错过了什么?



JSFiddle: http://jsfiddle.net/okohll/cKgAp/

/ jquery-click-event-fires-twice /rel =nofollow>这个网站双重使用 $(document).ready 原因。请尝试通过解除绑定您的函数来避免它:

  $('label.tiletype')。unbind ('click',
function(event){
event.stopPropagation(); // stop the .tile click being called
console.log('label clicked,x ='+ event .pageX +',y ='+ event.pageY);
//更多代码...
}


I have an element click event that's firing twice on a single click, a common issue from searching, but I've ruled out may of the potential causes from other answers. The code is

console.log("registering label click");
$("label.tiletype").click(
  function(event) {
    event.stopPropagation(); // stop the .tile click being called
    console.log("label clicked, x = " + event.pageX + ", y = " + event.pageY);
    // more code...
  }
}

When the page is loaded and the "label.tiletype" clicked, the log is

registering label click
label clicked, x = 679, y = 172
label clicked, x = 679, y = 172

Since "registering label click" only happens once, it can't be that the click is bound twice for any reason.

Since the pageX and pageY co-ordinates are the same, the double event occurs from the same physical click, not a second programatic click() being called on the element (I think, correct me if wrong).

The debug lines are only in this particular bit of code so there is no other click event firing.

I have checked the source in Firebug and there are multiple "label.tiletype" elements but none overlap on the screen, they are all display:block and float:left.

There is other stuff happening in the JavaScript file (line 375 https://github.com/okohll/agileBase/blob/200dd1c6c1a8968bc9d8f5b2b3d9188ac4776984/gtpb_clientside/resources/simple/simple.js) but whatever else is happening, the above statements must always be true logically.

This happens both in Firefox and Safari.

What have I missed?

JSFiddle: http://jsfiddle.net/okohll/cKgAp/

解决方案

according to this site a double use of $(document).readycould be the cause. try avoiding it by unbinding your function first:

$('label.tiletype').unbind('click').bind('click',
function(event) {
  event.stopPropagation(); // stop the .tile click being called
  console.log('label clicked, x = ' + event.pageX + ', y = ' + event.pageY);
  // more code...
}

这篇关于jQuery点击触发两次,但只注册一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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