簡體   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