How to remove the first two BR tags with jquery?

前端 未结 6 1265
萌比男神i
萌比男神i 2021-01-13 05:14

I\'d like to remove only the first two BR tags with jquery.



// I want to remove both BR. ... <
相关标签:
6条回答
  • 2021-01-13 05:28
    $("#system br:lt(2)").remove();
    
    0 讨论(0)
  • 2021-01-13 05:29

    Use :lt():

    $('#system br:lt(2)').remove();
    

    :lt() takes a zero-based integer as its parameter, so 2 refers to the 3rd element. So :lt(2) is select elements "less than" the 3rd.

    Example: http://jsfiddle.net/3PJ5D/

    0 讨论(0)
  • 2021-01-13 05:35

    Additionally if you only want to remove the last br only you can try;

    $('#elementID br:last').remove();
    

    Lets say that the br follows a , you can try

    $('#elementID strong+br:last').remove();
    
    0 讨论(0)
  • 2021-01-13 05:36

    Also try nth-child.

    $("#system > br:nth-child(1), #system > br:nth-child(2)").remove();​
    

    removes first and second instance of br within #system

    0 讨论(0)
  • 2021-01-13 05:41

    Try it. Replace (max 4 BR) to (1 BR)

    $('#system br').each(function() {
        if ($(this).next().is('br')) {
            $(this).next().remove();
        }
    });
    

    KS FIDDLE

    0 讨论(0)
  • 2021-01-13 05:43

    Try

    $('#system br:first').remove();
    $('#system br:first').remove();
    

    The first line removes the first br, then the second br becomes the first, and then you remove the first again.

    0 讨论(0)
提交回复
热议问题