简体   繁体   English

。附加() <li> 一个元素 <ul> 到另一个 <ul>

[英].append() <li> elements of one <ul> to another <ul>

Consider: 考虑:

<ul>
    <li class="abc">one</li>
    <li class="abc">two</li>
    <li class="abc">three</li>
</ul>

<ul class="target"></ul>

What exactly happens when I do the following? 当我执行以下操作时会发生什么?

$('.target').append($('.abc'));

I suspect all <li> elements to get removed from the first <ul> and get inserted into 'ul.target' . 我怀疑所有<li>元素都会从第一个<ul>删除并插入到'ul.target' Would the event handlers attached to <li> 's get removed? 附加到<li>的事件处理程序是否会被删除?

http://api.jquery.com/append/ http://api.jquery.com/append/

If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved into the target (not cloned): 如果以这种方式选择的元素插入到DOM中其他位置的单个位置,它将被移动到目标中(未克隆):

Presumably not cloned means event handlers don't get transferred. 据推测, not cloned意味着事件处理程序不会被转移。

However, it goes on to say: 然而,它继续说:

If there is more than one target element, however, cloned copies of the inserted element will be created for each target after the first. 但是,如果存在多个目标元素,则将在第一个目标元素之后为每个目标创建插入元素的克隆副本。

That means that because there are multiple elements, event handlers DO get transferred, as demonstrated here: 这意味着因为有多个元素,事件处理程序会被转移,如下所示:

http://jsbin.com/UMUHOVe/1/edit http://jsbin.com/UMUHOVe/1/edit

  <h1>list one</h1>
  <ul>
    <li class="abc">one</li>
    <li class="abc">two</li>
    <li class="abc">three</li>
</ul>

  <a href="#">Transfer</a>

  <h1>list two</h1>
<ul class="target"></ul>

$(document).ready(function() {
  $(".abc").each(function() {
    $(this).click(function() {
      console.log("hi");
    });
  });
  $("a").click(function(event) {
    event.preventDefault();
$('.target').append($('.abc'));
  });
});

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

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