简体   繁体   English

如何检测特定元素?

[英]How can I detect specific element is clicked?

Please follow these two steps: 请按照以下两个步骤操作:

  • focus on the input by clicking in it 通过单击它来关注input
  • and now click on the 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.

相关问题 如何检测我从数组中单击的元素 - How to detect the element i clicked on from an array 如何定位单击的 div 元素内的特定跨度? - How can I target a specific span inside a clicked div element? 即使单击后代,如何检测动态生成的元素的单击? - How can I detect a click on a dynamically generated element even when its descendant is clicked? 如何检测使用Javascript单击了类中的哪个元素? - How can you detect which element in a class was clicked on with Javascript? 如何检测父元素是否具有 javascript 的特定子元素? - how can i detect if a parent element has a specific child element with javascript? 如何检测单击的元素是否不是jQuery中元素的子级? - How to detect if clicked element was not a child of an element in jQuery? 如何使用 JQuery 在单击对象内检索具有特定类的 span 元素内的文本? - How can I retrieve the text inside a span element having a specific class that is inside a clicked object using JQuery? 我如何才能使div仅在jQuery中单击特定元素时显示? - How can I make a div appear only when a specific element gets clicked in jQuery? 当单击特定元素时,如何检测文本区域何时失去焦点并保持其失去焦点? - How can I detect when a text area loses focus and keep it from losing focus when a particular element is clicked? 我怎么能检测到什么时候 <select>点击android webview? - How can i detect when a <select> is clicked in android webview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM