通过classname删除所有span元素?
问题描述:
我有一个叫做notag
的类名。当我点击一个按钮时,它会触发我的onclick事件来调用我的函数。在函数中,我希望它删除/删除所有类名为notag
的span元素。通过classname删除所有span元素?
我的尝试是低于但没有运气!请帮忙谢谢。
onclick="replaceQueuePlaylist()"
function replaceQueuePlaylist()
{
$("span").attr('notag').remove(); //dont work
$('.notag').remove(); //also dont work
}
答
$(“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>
答
试试这个;
function replaceQueuePlaylist() {
$("span.notag").each(function() {
$(this).remove();
});
}
查找所有与notag类名称跨度并删除它们。
答
随着jQuery.remove()你可以直接删除所有集合从DOM匹配的元素:
function replaceQueuePlaylist() {
$('span.notag').remove();
}
答
在功能只需添加这些行。快乐编码
$("span.notag").each(function() {
$(this).remove();
});
'$('。notag')。remove();'should work,Can you share HTML? – Satpal
那是我的想法,但没有? :/ – irishwill200
所以你有另一个错误的地方。 –