javascript
  • jquery
  • tags
  • 2016-09-14 75 views -1 likes 
    -1

    我只是附加了一個A標籤,我試圖刪除它,謝謝你的任何幫助。jQuery如何刪除附加的A標籤與給定的ID

    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"); 
    } 
    
    +0

    使用'$( '#標題#一個Test1的')刪除();' – guradio

    +0

    我不想。也要刪除標題,只是標識爲Test1的A標籤。 – Ado

    +3

    *** [至少在API文檔](http://api.jquery.com/remove)中可以看到***這個問題。 –

    回答

    1

    $(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>

    使用$('#header a#Test1').remove();

    但你需要的是$('#Test1').remove()移除特定元素

    +0

    爲什麼'#header'需要在選擇器中去除? ID必須是唯一的 – charlietfl

    +0

    @charlietfl我只是從OP的原始派生它,但'$('#Test1')。remove()'就足夠了 – guradio

    +1

    ** #header a#Test1'的原因** if OP希望避免刪除元素,如果它不是'a'或它不在'#header'中。 –

    0

    你也可以用這個實現它:

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

    如果標題中還有其他元素不需要刪除,這將不起作用 – guradio

    +0

    我需要刪除特定的標記。 – Ado

    +0

    是的。同意你的意見 – Ish

    相關問題