为什么 mousedown 事件会取消我的 click 事件?

huangapple go评论52阅读模式
英文:

Why is the mousedown event getting rid of my click event?

问题

I am having an issue where my click event in a specific case is not being called. I can explain better using this example in the fiddle. I have also pasted the code below.

https://jsfiddle.net/qtdvn8oc/3/

<input type="button" id="openId" value="Open" />
<div id="qbr"></div>
$(document ).on("mousedown",function() {
  console.log("mousedown");
  $('#qbr').hide();
});

$("#openId").on("click", function() {
  console.log("open");
  $('#qbr').html("BLAH BLAH <input type=\"button\" id=\"closeId\" value=\"Close\" />");
  $('#qbr').show();
});

$(document).on('click', "#closeId", function(e){
  e.stopPropagation();
  console.log("close");
  $('#qbr').hide();
});

Look at the fiddle's console to see the issue I am facing.
When you click on the open button, you see the text and the close button. And it prints "mousedown" and "open" in the console.
Now when you click on the close button, you only see "mousedown" in the console. I need it to hit both mousedown and the close click events. This has to do with hiding the div on mousedown, but I do need that in place.
(The mousedown is there to close the div when you click anywhere on the document, think how a menu works).

Any help will be appreciated!

Thank you!

英文:

I am having an issue where my click event in a specific case is not being called. I can explain better using this example in the fiddle. I have also pasted the code below.

https://jsfiddle.net/qtdvn8oc/3/

&lt;html&gt;
    &lt;input type=&quot;button&quot; id=&quot;openId&quot; value=&quot;Open&quot; /&gt;
    &lt;div id=&quot;qbr&quot;&gt;&lt;/div&gt;
&lt;/html&gt;
$(document ).on(&quot;mousedown&quot;,function() {
  console.log(&quot;mousedown&quot;);
  $(&#39;#qbr&#39;).hide();
});

$(&quot;#openId&quot;).on(&quot;click&quot;, function() {
  console.log(&quot;open&quot;);
  $(&#39;#qbr&#39;).html(&quot;BLAH BLAH &lt;input type=\&quot;button\&quot; id=\&quot;closeId\&quot; value=\&quot;Close\&quot; /&gt;&quot;);
  $(&#39;#qbr&#39;).show();
});

$(document).on(&#39;click&#39;, &quot;#closeId&quot;, function(e){
  e.stopPropagation();
  console.log(&quot;close&quot;);
  $(&#39;#qbr&#39;).hide();
});

Look at the fiddle's console to see the issue I am facing.
When you click on the open button, you see the text and the close button. And it prints "mousedown" and "open" in the console.
Now when you click on the close button, you only see "mousedown" in the console. I need it to hit both mousedown and the close click events. This has to do with hiding the div on mousedown, but I do need that in place.
(The mousedown is there to close the div when you click anywhere on the document, think how a menu works).

Any help will be appreciated!

Thank you!

答案1

得分: 0

更新mousedown事件如下:

$(document).on("mousedown", function(e) {
  if (e.target.id === 'closeId') {
    return;
  }
  console.log("mousedown");
  $('#qbr').hide();
});
英文:

Update the mousedown event like below :

$(document ).on(&quot;mousedown&quot;,function(e) {
  if(e.target.id === &#39;closeId&#39;){
  return;
  }
  console.log(&quot;mousedown&quot;);
  $(&#39;#qbr&#39;).hide();
});

答案2

得分: 0

以下是您要翻译的代码部分:

For my solution, I see that the `mousedown` event listener was added to the entire document, including the `#qbr` div. In order for your code to work, you have to select the entire document excluding `#qbr` like so: `$(&quot;*:not(#qbr)&quot;)`

Then, for each element selected, we have to exclude the listener for child elements using this: `if (event.target !== event.currentTarget) return`
This is to make sure clicking on `#qbr` does not trigger the listener for `&lt;body&gt;`, or any other parent listener.

With this two changes the code will look something like this.

$(&quot;*:not(#qbr)&quot;).on(&quot;mousedown&quot;,function(event) {
  if (event.target !== event.currentTarget) return;
  event.stopPropagation();
  console.log(&quot;mousedown&quot;);
  $(&#39;#qbr&#39;).hide();
});

$(&quot;#openId&quot;).on(&quot;click&quot;, function() {
  console.log(&quot;open&quot;);
  $(&#39;#qbr&#39;).html(&quot;BLAH BLAH &lt;input type=\&quot;button\&quot; id=\&quot;closeId\&quot; value=\&quot;Close\&quot; /&gt;&quot;);
  $(&#39;#qbr&#39;).show();
})
$(document).on(&#39;click&#39;, &quot;#closeId&quot;, function(){
  console.log(&quot;close&quot;);
  $(&#39;#qbr&#39;).hide();
});
英文:

For my solution, I see that the mousedown event listener was added to the entire document, including the #qbr div. In order for your code to work, you have to select the entire document excluding #qbr like so: $(&quot;*:not(#qbr)&quot;)

Then, for each element selected, we have to exclude the listener for child elements using this: if (event.target !== event.currentTarget) return
This is to make sure clicking on #qbr does not trigger the listener for &lt;body&gt;, or any other parent listener.

With this two changes the code will look something like this.

$(&quot;*:not(#qbr)&quot;).on(&quot;mousedown&quot;,function(event) {
if (event.target !== event.currentTarget) return;
event.stopPropagation();
console.log(&quot;mousedown&quot;);
$(&#39;#qbr&#39;).hide();
});
$(&quot;#openId&quot;).on(&quot;click&quot;, function() {
console.log(&quot;open&quot;);
$(&#39;#qbr&#39;).html(&quot;BLAH BLAH &lt;input type=\&quot;button\&quot; id=\&quot;closeId\&quot; value=\&quot;Close\&quot; /&gt;&quot;);
$(&#39;#qbr&#39;).show();
})
$(document).on(&#39;click&#39;, &quot;#closeId&quot;, function(){
console.log(&quot;close&quot;);
$(&#39;#qbr&#39;).hide();
});

huangapple
  • 本文由 发表于 2023年3月23日 09:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定