javascript - 通过 css 删除 div 的用户脚本

标签 javascript jquery css userscripts

我想通过用户脚本删除一个 div,其中唯一可能不同的 div 是内联 css 中的免费图像。

假设一个 div 有以下 CSS: (背景图像:http://www.example.com/example.png)

有人可以帮我解决这个问题吗?

我已经尝试过以下一种但不起作用。

var badDivs = $("div div:contains('background-image')");
badDivs.remove ();

最佳答案

用途:

$("div").each(function() {
  if ($(this).css("background-image") != 'none') {
    $(this).remove();
  }
});

文档:.each() , .css() , .remove() .

警告!!检查所有 div 将是一项艰巨的工作,您应该使用像 toCheck 这样的类。所以:

$(".toCheck").each(function() {
  if ($(this).css("background-image") != 'none') {
    $(this).remove();
  }
});

工作中DEMO .

$(".toCheck").each(function() {
  if ($(this).css("background-image") != 'none') {
    $(this).remove();
  }
});
div {
  border:1px solid #000;
}

.toCheck {
  width: 100px;
  height: 100px;
}

#withImage {
  background-image: url("http://www.placehold.it/100/100");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="toCheck" id="withImage">
  Div to check with an image
</div>

<div class="toCheck">
  Div to check without an image
</div>

<div>
  Normal div
</div>

更新:

由于您的类 toCheck 是部分变量,因此您将需要一个更棘手的脚本,使用 Regular Expression 。首先,您需要扩展 JQUERY 选择器 ( tutorial ),因此对于 :regex :

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

然后将其与您的变量类一起使用:

$("div:regex(class, profile_view_img_+[0-9]*)").each(function() {
  if ($(this).css("background-image") != 'none') {
    $(this).remove();
  }
});

已更新DEMO .

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

$("div:regex(class, profile_view_img_+[0-9]*)").each(function() {
  if ($(this).css("background-image") != 'none') {
    $(this).remove();
  }
});
div {
  border:1px solid #000;
}

.toCheck {
  width: 100px;
  height: 100px;
}

#withImage {
  background-image: url("http://www.placehold.it/100/100");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="profile_view_img_22222222" id="withImage">
  Div to check with an image
</div>

<div class="profile_view_img_1111111">
  Div to check without an image
</div>

<div>
  Normal div
</div>

关于javascript - 通过 css 删除 div 的用户脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40537034/

相关文章:

javascript - 删除请求后重新加载当前路线

jquery - 在 .NET MVC4 中使用 Ajax 进行部分 View 加载

javascript - 在 sweetAlert 中输入文本

jquery - 有没有办法将变量添加到 jquery css 方法?

javascript - JQuery:如何为元素分配字体

javascript - JQuery - 如何 : Duplicate <li> and put it to the end of the <ul>

javascript - 使用 MongoDB 将多个数组聚合成一个大数组

javascript - 如何使用SounCloud API实现分页?

jQuery - 上下滑动特定距离

javascript - 如何使光标移动到按钮上并依次突出显示它们。使用像 .each 和 mouseenter 这样的 Jquery 方法