简体   繁体   English

开/关点击和jQuery的奇怪行为?

[英]Strange behavior with on/off click and jQuery?

so I'm trying to use link clicks to add buttons to a div, then be able to click those buttons to remove them and re-enable the original links to be clicked again so you can add and remove an infinite number of times. 因此,我尝试使用链接单击将按钮添加到div中,然后能够单击这些按钮将其删除,然后重新启用要再次单击的原始链接,以便可以添加和删除无数次。 I have it mostly working, but after trying to re-enable the click function I'm getting strange behavior. 我大多数情况下都能正常工作,但是尝试重新启用click功能后,我得到了奇怪的行为。

1) You need to click the link twice to re-append the button and 1)您需要单击两次链接以重新添加按钮,然后

2) Sometimes I'm getting multiple instances of the appended button in the div now. 2)有时我现在在div中获得附加按钮的多个实例。

Here is my code: 这是我的代码:

var selections = ' ';
    function add_Button() {
      jQuery(".form-unselected").click(function (e) {
        jQuery(this).removeClass('form-unselected').addClass('selected').off('click');
        var title = jQuery(this).attr("title");
        var id = jQuery(this).attr("href");
        selections += title + ', ';
        var button_content = jQuery('<button class="content-button">').html(title).attr("title", title).attr("id",id);
        event.preventDefault();
        $( "#selected-items" ).append(button_content);
        console.log(selections);
      });
    }
    add_Button();
    jQuery(document).on('click','.content-button', function(e){
      var removed_content = jQuery(this).attr("title") + ', ';
      selections =  selections.replace(removed_content,'');
      var href = jQuery(this).attr("id");
      jQuery(".add-to-form[href='"+ href +"']").addClass('form-unselected').removeClass('selected').on('click', add_Button );
      jQuery(this).remove();
      console.log(selections);
    });

The selections variable is a comma separated list of the values for another purpose, but I'm getting the duplicates there as well. selections变量是用逗号分隔的值列表,用于其他目的,但是我也在那里找到了重复项。 Thanks in advance! 提前致谢!

You should not dynamically add and remove the click handler. 您不应动态添加和删除点击处理程序。 Initially add the click handlers to all the buttons. 最初将单击处理程序添加到所有按钮。 Then when clicked you can query the status and decide. 然后单击时,您可以查询状态并做出决定。

Also there was an unknown reference event that did not match the argument e . 此外,还有一个未知的参考event ,该event与参数e不匹配。

And, repeating jQuery(this) is expensive. 而且,重复jQuery(this)非常昂贵。 Store this value in a local variable and refer to it instead. 将此值存储在局部变量中,然后引用它。 The code below demonstrates all the changes. 下面的代码演示了所有更改。

var selections = ' ';
      jQuery(".form-unselected").click(function (e) {
        var $this = jQuery(this);
        if ($this.hasClass("selected")) {
          return;
        }
        $this.removeClass('form-unselected').addClass('selected');
        var title = $this.attr("title");
        var id = $this.attr("href");
        selections += title + ', ';
        var button_content = jQuery('<button class="content-button">').html(title).attr("title", title).attr("id",id);
        e.preventDefault();
        $("#selected-items").append(button_content);
        console.log(selections);
      });
    jQuery(document).on('click','.content-button', function(e) {
      var $this = jQuery(this);
      var removed_content = $this.attr("title") + ', ';
      selections =  selections.replace(removed_content,'');
      var href = $this.attr("id");
      jQuery(".add-to-form[href='"+ href +"']").addClass('form-unselected').removeClass('selected');
      $this.remove();
      console.log(selections);
    });

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

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