1

我需要从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 做些什么改变?

4

4 回答 4

0

尝试这个:

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

这是文档

于 2013-06-05T12:05:08.443 回答
0
$('#btn').click(function(){
   $("#testdiv").remove("a[dir='hello']");
})
  • 你不需要if
  • 您忘记了remove声明中的引号。
  • 你忘了最后一个)
于 2013-06-05T12:05:11.413 回答
0

尝试这个:

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

于 2013-06-05T12:06:51.030 回答
0

它正在工作:

$(function(){
        $('#btn').click(function(){
                if($("#testdiv").find("a[dir='hello']").length=='1'){
                    $("#testdiv").find("a[dir='hello']").remove();
                }
        });
    });
于 2013-06-05T12:26:16.890 回答