繁体   English   中英

Javascript / jQuery:输入“模糊”可防止触发“点击”

[英]Javascript/jQuery: input 'blur' prevents 'click' being fired

jsBin
http://jsbin.com/eyARuHI/2/edit?html,css,js,输出

代码

$('#in').on('blur', function(event) {
  alert('input: ' + event.type);
});
$('#button').on('click', function(event) {
  alert('button: ' + event.type);
});

HTML

<input id='in' type='text'>
<div id='button'></div>

问题
如果专注于输入,然后单击按钮-永远不会触发click

在这种情况下如何正确处理点击事件?

如@RGraham所建议,请尝试跟随JS。 您将能够看到每次单击按钮事件都会被触发。

$('#in').on('blur', function(event) {
  console.log('input: ' + event.type);
});
$('#button').on('click', function(event) {
  console.log('button: ' + event.type);
});

您将需要停止冒泡事件。 尝试这个

$('#button').on('click', function(event) {
  alert('button: ' + event.type);
  return false;
});


$('#in').on('blur', function(event) {
  alert('input: ' + event.type);
});

暂无
暂无

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

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