Home > Web Front-end > JS Tutorial > JQuery's method of copying DOM nodes_jquery

JQuery's method of copying DOM nodes_jquery

WBOY
Release: 2016-05-16 15:55:56
Original
1308 people have browsed it

The example in this article describes how JQuery copies DOM nodes. Share it with everyone for your reference. The specific analysis is as follows:

Copying nodes is also one of the commonly used DOM operations, such as the effect of many shopping websites. Users can not only click the "Select" button below the product to purchase the corresponding product, but also drag the product with the mouse and place it on the in shopping cart. This product dragging function uses a copy node to copy the node element of the product selected by the user and move it with the mouse to achieve the shopping effect.

HTML DOM structure is as follows:

<p class="nm_p" title="欢迎访问脚本之家" >欢迎访问脚本之家</p>
<ul class="nm_ul">
  <li title='PHP编程'>简单易懂的PHP编程</li>
  <li title='C编程'>简单易懂的C编程</li>
  <li title='JavaScript编程'>简单易懂的JavaScript编程</li>
  <li title='JQuery'>简单易懂的JQuery编程</li>
</ul>

Copy after login

If you need to copy another

  • element after clicking the
  • element, you can use the clone() method to complete it.

    The JQuery code is as follows:

    $(function(){
      $(".nm_ul li").click(function(){
        $(this).clone(true).appendTo(".nm_ul");
        // 复制当前点击的节点,并将它追加到<ul>元素
      }) 
    });
    Copy after login

    After clicking any item on the page, a new node for that item will appear at the bottom of the list.

    After copying a node, the copied new element does not have any behavior. If you need the new element to also have copy functionality (click event in this case), you can use the following JQuery code:

    $("ul li").click(function(){ 
     $(this).clone(true).appendTo("ul"); // 注意参数true 
     //可以复制自己,并且他的副本也有同样功能。 
    })
    Copy after login

    A parameter true is passed in the clone() method, which means that when copying the element, the events bound to the element are also copied. So the copy of the element also has copy functionality (in this case the click event).

    I hope this article will be helpful to everyone’s jQuery programming.

  • Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template