简体   繁体   中英

jQuery how to remove an appended A tag with a given id

I just appended an A tag and I am trying to remove it, thank you for any help.

HTML:

<div>
    <p id="header"></p>
</div>

jQuery:

$(document).ready(function (e) {
    // Append A tag with id= Test1
    $('#header').append("<a href='#' id='Test1'> Test1</a>");

    // Remove the tag - it doesn't work.
    $('#header').remove("a#Test1");
}

 $(document).ready(function(e) { // Append A tag with id= Test1 $('#header').append("<a href='#' id='Test1'> Test1</a>"); // Remove the tag - it doesn't work. $('#header a#Test1').remove(); })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <p id="header"></p> </div>

Use $('#header a#Test1').remove();

but all you need is $('#Test1').remove() to remove that specific element

您也可以通过以下方式实现:

$('#header').empty();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM