简体   繁体   English

即使我使用的是 :not 选择器,也会触发事件侦听器

[英]Event listener is triggered even though I'm using :not selector

 $('.user-card:not(.loaded)').click(function(e) { $(e.target).addClass('loaded'); $(e.target).text('loaded!'); console.log('this is now loaded'); }); $('.loaded').click(function() { alert('already loaded!') });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" class="user-card"> not loaded </button> <button type="button" class="user-card"> not loaded </button>

I don't want the "loaded!"我不想要“加载!” button to acctivate the '.user-card:not(.loaded)' event listener, yet it does.按钮激活'.user-card:not(.loaded)'事件侦听器,但它确实如此。 Why is this?为什么是这样?

The click event will fire every time because you aren't reassigning click events after each click.每次点击后都会触发点击事件,因为您不会在每次点击后重新分配点击事件。 You have multiple choices on how to address this.关于如何解决这个问题,您有多种选择。 One way I have here is to look at the "loaded" class on the click.我在这里的一种方法是在单击时查看“加载”类。 If it does not have the class "loaded" then it runs the code.如果它没有“加载”类,则它运行代码。 Otherwise the alert fires to let you know the element has already been loaded.否则警报会触发,让您知道元素已经加载。

 $('.user-card:not(.loaded)').click(function(e) { if (!$(e.target).hasClass('loaded')) { $(e.target).addClass('loaded'); $(e.target).text('loaded!'); console.log('this is now loaded'); } else { alert('already loaded!'); } }); $('.loaded').click(function() { alert('already loaded!'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" class="user-card"> not loaded </button> <button type="button" class="user-card"> not loaded </button> <button type="button" class="user-card loaded"> loaded </button>

通过从父级继承属性,两个按钮都“未加载”,请改用“.hasClass”

暂无
暂无

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

相关问题 注册多个事件侦听器,即使我正在使用removeEventListener - Registering multiple event listeners even though I'm using removeEventListener Javascript“变量声明但从未使用过”......即使我正在使用它? - Javascript “variable declared but never used”… even though I'm using it? 为什么即使我不使用切换,.show()和.hide()也会切换? - Why are .show() and .hide() toggling even though I'm not using toggle? 即使我引用了JavaScript函数,事件监听器也会多次调用JavaScript函数 - JavaScript function gets called multible times by event listener even though I reference the function 在设置侦听器之前会触发事件吗? - Will event be triggered before I set up the listener? 即使事件触发,jQuery简单的工具提示也不会显示? - jQuery simple tooltip not showing even though event triggered? 发出事件$ emit时,Vue $ on无法运行我的函数,即使我可以在Vue控制台中看到触发的事件 - Vue $on not running my function when event $emit is emited, even though I can see the event triggered in Vue console 即使鼠标悬停在Google Maps侦听器事件上,其行为也类似于单击 - Google maps listener event acts like a click even though it is a mouseover 为什么即使我使用的是 Set,我的表上的两列也会出现重复值? - Why am I seeing repeated values for both columns on my table even though I'm using a Set? Javascript:即使我使用 window.onload 我也收到错误“var is not defined” - Javascript: even though I'm using window.onload I get the error 'var is not defined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM