Link list sidebar widget remove button not working in jQuery.

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

Link list sidebar widget remove button not working in jquery

问题

我正在创建一个链接列表小部件。每当我们点击标题按钮时,它会添加一个带有添加链接选项的列表。一切都运行正常,但是每当我尝试点击删除标题按钮时,按钮不再起作用。警告是有效的,控制台中也显示日志,但是操作不起作用。你能帮忙检查一下吗?

Code Pen Codepen参考链接

英文:

I am creating a link list widget. Whenever we clicked on Title button, it adds a list with add link option. Everything is working fine but whenever I am trying to click the remove title button. The button is not working anymore. Where the alert is working and logs are showing also in console but the action is not working. Can you please check?

Code Pen Codepen Reference Link

答案1

得分: 0

在第23行,您只选择父HTML元素,当点击“remove-title”按钮时,但不执行任何操作。

首先,您应该确保您不要删除唯一剩下的部分。如果是这样,会先触发.add-title按钮。

如您所见,我还向btn-options div添加了一个“toast”元素,以简要显示已删除部分的标题。您可以在这里找到其余的更改:Codepen

英文:

At line 23 you are only selecting a parent HTML element when the remove-title button is clicked, but doing nothing with it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

if ($(&quot;.remove-title&quot;).length) {
  $(&quot;body&quot;).on(&quot;click&quot;, &quot;.remove-title&quot;, function() {
    console.log(&quot;clicked&quot;);
    $(this).parents(&quot;.btn-options&quot;);
  });
}

<!-- end snippet -->

First you should make sure you're not deleting the only section left. If so, the .add-title button is triggered first.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

if ($(&quot;.remove-title&quot;).length) {
  $(&quot;body&quot;).on(&quot;click&quot;, &quot;.remove-title&quot;, function() {
    console.log(&quot;clicked&quot;);
    var $this = $(this);
    var parent = $this.parents(&quot;.title-area&quot;);
    if (!parent.siblings(&quot;.title-area&quot;).length) {
      $this.siblings(&quot;.add-title&quot;).trigger(&quot;click&quot;);
    }
    var title = parent.find(&quot;input.title-text&quot;).eq(0).val();
    var toast = parent.siblings(&quot;.title-area&quot;).find(&quot;.btn-toast&quot;);
    toast.show().html(&quot;Title removed: &lt;span&gt;&quot; + title + &quot;&lt;/span&gt;&quot;)
      .delay(400).fadeOut(&quot;slow&quot;);
    parent.remove();
  });
}

<!-- end snippet -->

As you can see, I've also added a "toast" element to the btn-options div to briefly display the title of the deleted section. You can find the rest of those changes here: Codepen

huangapple
  • 本文由 发表于 2023年6月2日 00:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383772.html
匿名

发表评论

匿名网友

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

确定