jQuery的appendTo()问题

发布于 2024-09-26 19:57:10 字数 1506 浏览 3 评论 0原文

我在将列表项从一个 ul 附加到另一个 ul 时遇到问题。当每个列表项被单击时,它应该被附加到另一个 ul 中。然而,一旦附加这些项目,它们将继续将自己附加到当前的 ul....这意味着它们会将自己排序到列表的底部。例如:

<ul class="first-holder">
            <li><input type="checkbox" name="location1"  /> Location 1</li>
            <li><input type="checkbox" name="location2"  /> Location 2</li>
            <li><input type="checkbox" name="location3"  /> Location 3</li>
            <li><input type="checkbox" name="location4"  /> Location 4</li>
            <li><input type="checkbox" name="location5"  /> Location 5</li>


            </ul>


            <div class="more-options first-option">
            <ul>
            <li><input type="checkbox" name="location-6" checked="checked" /> Location 6</li>
            <li><input type="checkbox" name="location7" checked="checked" /> Location 7</li>
            <li><input type="checkbox" name="location8" checked="checked" /> Location 8</li>
            </ul>
            </div>

jquery:

$('div.more-options li').click(function() {

                $(this).appendTo($('ul.first-holder'));
                return false;

           }); 

发生的情况是这样的: div.more-options 中的列表项将追加到 ul.first-holder,但是当这些项位于 ul.first-holder 中时,单击它们将导致它们追加到 ul.first-holder 的末尾。一旦 li 被附加到 ul.first-holder,它们就不应该移动到任何地方。我不知道如何实现这一点。

I am having a problem appending list items from one ul to another. When each list item is clicked, it is supposed to be appended into another ul. However once those items are appended, they will continue to append themselves into their current ul....meaning they will sort themselves to the bottom of the list. For example:

<ul class="first-holder">
            <li><input type="checkbox" name="location1"  /> Location 1</li>
            <li><input type="checkbox" name="location2"  /> Location 2</li>
            <li><input type="checkbox" name="location3"  /> Location 3</li>
            <li><input type="checkbox" name="location4"  /> Location 4</li>
            <li><input type="checkbox" name="location5"  /> Location 5</li>


            </ul>


            <div class="more-options first-option">
            <ul>
            <li><input type="checkbox" name="location-6" checked="checked" /> Location 6</li>
            <li><input type="checkbox" name="location7" checked="checked" /> Location 7</li>
            <li><input type="checkbox" name="location8" checked="checked" /> Location 8</li>
            </ul>
            </div>

The jquery:

$('div.more-options li').click(function() {

                $(this).appendTo($('ul.first-holder'));
                return false;

           }); 

What happens is this: The list items in the div.more-options will append to ul.first-holder, but then when those items are in ul.first-holder, clicking them will cause them to append to the end of ul.first-holder. Once the li's have been appended to ul.first-holder they shouldn't move anywhere. I can't figure out how to make that happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他夏了夏天 2024-10-03 19:57:10

尝试从项目中删除事件处理程序。它将停止接收点击事件。

$(this).unbind('click');
$(this).appendTo($('ul.first-holder'));

http://api.jquery.com/unbind/

Try removing event handler from the item. It will just stop receiving click events.

$(this).unbind('click');
$(this).appendTo($('ul.first-holder'));

http://api.jquery.com/unbind/

顾忌 2024-10-03 19:57:10

这是因为您正在移动附加了单击事件的节点。因此,当该节点附加到另一个 ul 时,单击事件仍然适用。

您可以复制节点并追加该节点,也可以在追加时删除单击事件。

That's because you are moving the node with the click event attached to it. So when that node is appended to the other ul, the click event still applies.

You can either copy the node and append that, or remove the click event when appending.

Spring初心 2024-10-03 19:57:10

在这里使用 .delegate() ,如下所示:

$('div.more-options').delegate('li', 'click', function() {
  $('ul.first-holder').append(this);
}); 

您可以在此处进行测试

目前,您的 click 处理程序位于

  • 元素上,并随它们一起移动...而是将 click 处理程序放在 >.more-options
    本身,因此没有要移动的处理程序。如果您有多个元素,执行起来也会更便宜:)
  • Use .delegate() here instead, like this:

    $('div.more-options').delegate('li', 'click', function() {
      $('ul.first-holder').append(this);
    }); 
    

    You can test it out here.

    Currently your click handlers are on the <li> elements, and moving with them...instead put the click handler on the .more-options <div> itself, so there is no handler to move. This is also cheaper to execute if you have more than a few elements :)

    和影子一齐双人舞 2024-10-03 19:57:10

    您只想处理第一个 click 事件,如下所示:

    $('div.more-options li').one('click', function() { ... }):
    

    You want to only handle the first click event, like this:

    $('div.more-options li').one('click', function() { ... }):
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文