我有类似的东西
最佳答案
这样更有效,因为它只选择具有data-count属性的div,并且只为每个元素构造一次jQuery对象:
$('div[data-count]').each(function () {
var $this = $(this);
if ($this.attr('data-count') < 15) {
$this.remove();
}
});
演示:
$('button').on('click',function () {
$('div[data-count]').each(function () {
var $this = $(this);
if ($this.attr('data-count') < 15) {
$this.remove();
}
});
});
div[data-count]:before {
content: "
本机JavaScript中的替代解决方案:
var divs = document.querySelectorAll('div[data-count]'),div,i;
for (i = 0; i < divs.length; i++) {
div = divs[i];
if (div.getAttribute('data-count') < 15) {
// this will not affect iteration because querySelectorAll is a non-live NodeList
div.parentNode.removeChild(div);
}
}
演示:
var button = document.querySelector('button');
button.addEventListener('click',function () {
var divs = document.querySelectorAll('div[data-count]'),i;
for (i = 0; i < divs.length; i++) {
div = divs[i];
if (div.getAttribute('data-count') < 15) {
// this will not affect iteration because querySelectorAll is a non-live NodeList
div.parentNode.removeChild(div);
}
}
});
div[data-count]:before {
content: "
原文链接:https://www.f2er.com/html/426557.html