2016-11-11 122 views
0

我有一个叫做notag的类名。当我点击一个按钮时,它会触发我的onclick事件来调用我的函数。在函数中,我希望它删除/删除所有类名为notag的span元素。通过classname删除所有span元素?

我的尝试是低于但没有运气!请帮忙谢谢。

onclick="replaceQueuePlaylist()" 

function replaceQueuePlaylist() 
{ 
    $("span").attr('notag').remove(); //dont work 
    $('.notag').remove(); //also dont work 
} 
+4

'$('。notag')。remove();'should work,Can you share HTML? – Satpal

+0

那是我的想法,但没有? :/ – irishwill200

+2

所以你有另一个错误的地方。 –

回答

5

$(“span”)。attr('notag')。remove(); //不工作

这是行不通的,因为你的元素有一个名为notag类,而不是属性。 class本身是<div class='notag'> hello world </div>

与值notag属性没有必要明确地使用.each()如使用$(selector).remove()将通过与选择条件的每个自​​动元素迭代并删除它。

$('#delete-em').on('click', function(){ 
 
    $('.delete-me').remove(); 
 
});
.delete-me{ 
 
    height: 50px; 
 
    width: 50px; 
 
    margin: 5px; 
 
    background: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class='delete-me'></div> 
 
<div class='delete-me'></div> 
 
<div class='delete-me'></div> 
 
<div class='delete-me'></div> 
 

 
<button id="delete-em">Remove</button>

1

试试这个;

function replaceQueuePlaylist() { 
    $("span.notag").each(function() { 
     $(this).remove(); 
    }); 
} 

查找所有与notag类名称跨度并删除它们。

1

随着jQuery.remove()你可以直接删除所有集合从DOM匹配的元素:

function replaceQueuePlaylist() { 
    $('span.notag').remove(); 
} 
0

在功能只需添加这些行。快乐编码

$("span.notag").each(function() { 
    $(this).remove(); 
});