[英]How can I detect specific element is clicked?
Please follow these two steps: 请按照以下两个步骤操作:
input
by clicking in it input
button
button
$("input").on({ focusout: function() { this.value += "one|"; } }); $("button").on("click", function(){ $old_val = $("input").val(); $("input").val($old_val+"two|"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" /> <button>Add "two"</button>
The result will be: one|two|
结果将是:
one|two|
! ! Well I don't want this.
好吧,我不想要这个。 I want to disable
focusout
when I click on the button. 我想在点击按钮时禁用
focusout
。 How can I do that? 我怎样才能做到这一点?
Expected result after following those two steps should be two|
遵循这两个步骤之后的预期结果应该是
two|
. 。
You want to prevent the button from calling the default mousedown
event which is where browsers change the focus. 您希望阻止按钮调用默认的
mousedown
事件,这是浏览器更改焦点的位置。 Using e.preventDefault()
will stop this from happening if you assign it to the mousedown
event on the button. 如果将其分配给按钮上的
mousedown
事件,则使用e.preventDefault()
将阻止此操作发生。
$("input").on({ focusout: function() { this.value += "one|"; } }); $("button").on("mousedown", function(e) { e.preventDefault(); }); $("button").on("click", function(){ $old_val = $("input").val(); $("input").val($old_val+"two|"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" /> <button>Add "two"</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.