簡體   English   中英

如何刪除具有特定屬性值的HTML元素?

[英]How do I remove HTML elements with specific attribute value?

我需要刪除a從具有特定屬性值的標簽div標簽。

例如:

<div id='testdiv'>
    <a dir='hello' href='#' ></a>
    <a dir='how' href='#' ></a>
    <a dir='which' href='#' ></a>
</div>
<input type='button' id='btn' />

這是我的jQuery:

$('#btn').click(function(){
   if($("#testdiv").find("a[dir='hello']").length == 1)
   {
       $("#testdiv").remove("a[dir=hello]");
   }
}

但是它不起作用。 我需要對jQuery做哪些更改?

嘗試這個:

$("a[dir=hello]").remove();

這是文檔

$('#btn').click(function(){
   $("#testdiv").remove("a[dir='hello']");
})
  • 你不需要if
  • 您忘記了remove語句中的引號。
  • 你忘了最后)

嘗試這個:

$('a[dir="hello"]').remove

工作正常:

$(function(){
        $('#btn').click(function(){
                if($("#testdiv").find("a[dir='hello']").length=='1'){
                    $("#testdiv").find("a[dir='hello']").remove();
                }
        });
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM