jQuery: What's the difference between after() and insertAfter()

后端 未结 11 666
孤街浪徒
孤街浪徒 2021-01-31 07:42

jQuery has an .after() method, and also an .insertAfter() method.

What\'s the difference between them? I think I can use .after()

11条回答
  •  迷失自我
    2021-01-31 08:00

    All of the answers so far are clear as mud ;-) (So I'll take a stab at it too!)

    If you start off with this Html:

    Para 1

    Para 2 More

    After inserts some new content after the matching tags:

    $("p")                       // Match all paragraph tags
        .after("Hello");  // Insert some new content after the matching tags
    

    The end result is:

    Para 1

    Hello

    Para 2 More

    Hello

    On the other hand, InsertAfter moves one or more elements which already exist on the DOM after the selected elements (Really, this method could be called MoveAfter):

    $("#sMore")                    // Find the element with id `sMore`
        .insertAfter("#pOne");     // Move it to paragraph one
    

    Resulting in:

    Para 1

    More

    Para 2

提交回复
热议问题