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

后端 未结 11 665
孤街浪徒
孤街浪徒 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:08

    One important things apart some syntax is that memory leak and performance. insertafter is more efficient than insert when you have tons of dom elements. So prefer insertafter instead of after.

    0 讨论(0)
  • 2021-01-31 08:13

    They are inverses of each other. As explained in the jQuery documentation:

    This:

    $("p").insertAfter("#foo");
    

    Is the same as this:

    $("#foo").after("p");
    

    And lastly, insertAfter returns all inserted elements, whereas .after() will return the context it is called on.

    0 讨论(0)
  • 2021-01-31 08:16
    $("p").insertAfter("#foo");
    

    ==

    $("#foo").after("p")
    
    0 讨论(0)
  • 2021-01-31 08:18

    Check the documentation:

    $("#foo").after("p")
    

    is the same as:

    $("p").insertAfter("#foo");
    
    0 讨论(0)
  • 2021-01-31 08:21

    ( after & before ):

    $('selector').after('new_content');
    $('selector').before('new_content');
    

    while ( insertAfter & insertBefore ):

    $('new_content').insertAfter('selector');
    $('new_content').insertBefore('selector');
    
    0 讨论(0)
提交回复
热议问题