简体   繁体   English

jquery 在 div 上聚合 onclick

[英]jquery aggregates onclick on div

I have a div where I on pageload binds a click function like below我有一个 div,我在页面加载时绑定了一个点击 function,如下所示

 <div class="document-short-list-item" data-d-id="5" data-w-id="16" data-s-id="15">Klik her for at tilføje</div>

$(".document-short-list-item").click(function () {
    addToShortList(this);
});

The addToShortList performs the following: addToShortList 执行以下操作:

  $(element).removeClass('document-short-list-item').addClass('document-short-list-item-added').on('click', removefromShortList(element));

and the removefromShortList reverses the same并且 removefromShortList 反转相同

 $(element).removeClass('document-short-list-item-added').addClass('document-short-list-item').on('click', addToShortList(element));

The problem is that the above code aggregates on each click, this means the both addToShortList and removeFromShortList runs multiple times on click问题是上述代码在每次点击时都会聚合,这意味着 addToShortList 和 removeFromShortList 在点击时都会运行多次

Because every click event calls another .on() to add more handlers.因为每个点击事件都会调用另一个.on()来添加更多处理程序。

It's almost always a bad idea to bind/unbind handlers in their own binding events and often leads to this kind of situation.在自己的绑定事件中绑定/取消绑定处理程序几乎总是一个坏主意,并且经常导致这种情况。 Instead, you can rely on the functionality of .on() to dynamically execute as the classes change.相反,您可以依靠.on()的功能在类更改时动态执行。 This is called "event delegation".这称为“事件委托”。

Basically bind the click event to a common parent element ( document often works, unless anything is stopping the event propagation).基本上将点击事件绑定到一个公共父元素( document通常有效,除非有任何事情阻止事件传播)。 Something like this:像这样的东西:

$(document).on("click", ".document-short-list-item", function () {
    $(this)
        .removeClass("document-short-list-item")
        .addClass("document-short-list-item-added");
});
$(document).on("click", ".document-short-list-item-added", function () {
    $(this)
        .removeClass("document-short-list-item-added")
        .addClass("document-short-list-item");
});

This will attach the click handler to the document itself, but when handling that click it will filter the originating element by the selector argument.这会将点击处理程序附加到文档本身,但是在处理该点击时,它将通过选择器参数过滤原始元素。 So any originating .document-short-list-item will invoke the first handler, and any originating .document-short-list-item-added will invoke the second handler.因此,任何原始.document-short-list-item都将调用第一个处理程序,任何原始.document-short-list-item-added将调用第二个处理程序。 This way you only need to set these handlers once on page load and they internally filter as needed.这样,您只需在页面加载时设置这些处理程序一次,它们会根据需要在内部进行过滤。


Alternatively, if this approach might work in your situation, you can use a single handler which will simply toggle the classes.或者,如果这种方法可能适用于您的情况,您可以使用单个处理程序来简单地切换类。 For example:例如:

$(document).on("click", ".document-short-list-item, .document-short-list-item-added", function () {
    $(this)
        .toggleClass("document-short-list-item")
        .toggleClass("document-short-list-item-added");
});

Which would respond to either originating element (since both are in the selector) and toggle both classes, regardless of which one is currently active.它将响应任一原始元素(因为两者都在选择器中)并切换两个类,无论哪个当前处于活动状态。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM