[英]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做哪些更改?
$('#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.