Append, AppendTo, prepend, Prependto, before, InsertBefore, after, InsertAfter, ReplaceAll methods are called, the elements that were originally displayed on the page disappear

Source: Internet
Author: User

Detailed Description:

  $ele. Append,. AppendTo,. prepend,. Prependto,. Before,. InsertBefore,. After,. InsertAfter,. ReplaceAll following these method calls, If the object being added is the original object on the page, when these methods are called, the original object disappears and appears in a new place (note: The object here is a jquery object)

Phenomenon:

  Code:

<body>        <ul>            <li>num:1</li>            <li>num:2</li>            <li>num:3 </li>            <li>num:4</li>            <li>num:5</li>        </ul>        <script>            var $li = $ ("<span>~~ label ~~</span>")            $li. AppendTo ($ ("Li"));            $ ("LI:GT (2)"). Append ($li);         </script>    </body>

  

  Problem:

    Why don't add two span tags behind NUM5

  Reason:

$ele. Append, AppendTo, prepend, Prependto, before, InsertBefore, after, InsertAfter, ReplaceAll
If the object being added is the original object on the page, when these methods are called, the original object disappears and appears in a new place (note: The object here is a jquery object)

  Workaround:

1. First line: Create label; second line: encapsulate the label as a jquery object and add it; the third line: Add the label directly to the corresponding    jquery object var li = "<span>~~ label ~~</ Span> ";    $ (LI). AppendTo ($ ("Li"));     $ ("LI:GT (2)"). Append (li);             2. The first line: Create a jquery object, the second line: Add the object, and the third line: Reassign the value to $li, which is an object that is not displayed on the page (and therefore does not consider the problem of disappearing); line four: Add     the re-assigned $li var $li = $ ("<span>~~ tag ~~</span>");    $li. AppendTo ($ ("Li"));     = $ ("<span>~~ label ~~</span>");    $ ("LI:GT (2)"). Append ($li);

Append, AppendTo, prepend, Prependto, before, InsertBefore, after, InsertAfter, ReplaceAll methods are called, the elements that were originally displayed on the page disappear

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.