jquery 检查短语是否以DIV样式存在

fquxozlt  于 2023-05-17  发布在  jQuery
关注(0)|答案(3)|浏览(121)

所以我有一个包含背景图像的DIV。在某些情况下,这个图像是“直接”设置的,在其他情况下,它是生成的SVG。我希望能够识别DIV何时包含SVG字符串。
我的两个部门:

<div class="cover my-1" style="background-image: url('something.jpg');">Div 1 QQ</div>
<div class="cover my-2" style="background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc...=');">Div 2</div>

我试着这样做:

$(document).ready(function() {
  $(".cover.my-1:contains(QQ)").css("background-color", "yellow");
  $(".cover.my-2:contains(svg+xml)").css("background-color", "yellow");  
});

因此,DIV 1随着QQ的发现而改变。DIV 2不会改变,因为在DIV字段中找不到“svg+xml”(但在样式中)。如何识别DIV 2中的“svg+xml”?
我希望在DIV的样式中识别“svg+xml”

bd1hkmkf

bd1hkmkf1#

:contains只对元素的内容起作用。您可以使用filterincludes来选择带有svg背景的项目

$(document).ready(function(){
  $(".cover.my-1:contains(QQ)").css("background-color", "yellow");
 $(".cover.my-2").filter((_, el) => $(el).css("background-image").includes("svg+xml")).css("background-color", "yellow");  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cover my-1" style="background-image: url('something.jpg');">Div 1 QQ</div>
<div class="cover my-2" style="background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc...=');">Div 2</div>
<div class="cover my-2" style="background-image: url('something.jpg');">Div 2 with class .cover.my-2, but without svg background is not selected</div>
vaqhlq81

vaqhlq812#

如果你事先知道你的svg+xml总是在style属性中,你可以使用[attr*=value] attribute selector。这将检查value是否存在于attr中。

$(document).ready(function() {
  $(".cover.my-1:contains(QQ)").css("background-color", "yellow");
  $('.cover.my-2[style*="svg+xml"]').css("background-color", "yellow");  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="cover my-1" style="background-image: url('something.jpg');">Div 1 QQ</div>
<div class="cover my-2" style="background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc...=');">Div 2</div>
fcg9iug3

fcg9iug33#

这个代码为我工作。如果包含data:image/svg+xml,则将文本颜色变为红色,如果在background-url链接中没有data:image/svg+xml,则将文本颜色变为黑色

**这里是一个例子:**您可以运行下面的片段并签入此片段DIV包含SVG背景图像,因此在文档加载时,DIV的文本将为红色

$(document).ready(function(){
  if($('.cover').attr("style").indexOf('data:image/svg+xml') != -1){
    $('.cover').css({'color':'#ff0000'});
  }else{
    $('.cover').css({'color':'#000000'});
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

<div class="cover" style="background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iMCAwIDEwMCAxMDAiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDEwMCAxMDAiIHhtbDpzcGFjZT0icHJlc2VydmUiIGhlaWdodD0iMTAwcHgiIHdpZHRoPSIxMDBweCI+CjxnPgoJPHBhdGggZD0iTTI4LjEsMzYuNmM0LjYsMS45LDEyLjIsMS42LDIwLjksMS4xYzguOS0wLjQsMTktMC45LDI4LjksMC45YzYuMywxLjIsMTEuOSwzLjEsMTYuOCw2Yy0xLjUtMTIuMi03LjktMjMuNy0xOC42LTMxLjMgICBjLTQuOS0wLjItOS45LDAuMy0xNC44LDEuNEM0Ny44LDE3LjksMzYuMiwyNS42LDI4LjEsMzYuNnoiLz4KCTxwYXRoIGQ9Ik03MC4zLDkuOEM1Ny41LDMuNCw0Mi44LDMuNiwzMC41LDkuNWMtMyw2LTguNCwxOS42LTUuMywyNC45YzguNi0xMS43LDIwLjktMTkuOCwzNS4yLTIzLjFDNjMuNywxM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uMiwyMy4yLDg2LjMsMjYuMiw4OC4yeiIvPgoJPHBhdGggZD0iTTMwLjksNzNjMi45LDYuOCw2LjEsMTQuNCwxMC41LDIxLjJjMTUuNiwzLDMyLTIuMyw0Mi42LTE0LjZDNjcuNyw3Niw1Mi4yLDY5LjYsMzcuOSw2MC43QzMyLDU3LDI2LjUsNTMsMjEuMyw0OC42ICAgYy0wLjYsMS41LTEuMiwzLTEuNyw0LjZDMjQuMSw1Ny4xLDI3LjMsNjQuNSwzMC45LDczeiIvPgo8L2c+Cjwvc3ZnPg=='); width:100px; height:100px; color:#000">Div 2</div>

**这里是一个例子:**您可以运行下面的片段并签入此片段DIV包含PNG背景图像,因此在文档加载时,DIV的文本将为黑色

$(document).ready(function(){
  if($('.cover').attr("style").indexOf('data:image/svg+xml') != -1){
    $('.cover').css({'color':'#ff0000'});
  }else{
    $('.cover').css({'color':'#000000'});
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

<div class="cover" style="background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAeFBMVEX///8AAABnZ2fBwcHX19eWlpby8vKSkpLp6en8/Pza2tr5+flubm6Pj4/29vbFxcV+fn5eXl4tLS2ysrJ4eHi6uro7OzsiIiKbm5vh4eG8vLwnJyeFhYVMTEzR0dExMTE/Pz9WVlalpaUVFRVOTk4ODg4bGxs9PT0Tbfj/AAAK4UlEQVR4nN1daUPqOhAF2aEIWBZBtgJy//8/fFauCsKcmSwz6X3nqzXtIcnsydRq2hi0+428tR5nm/nL4vher78fz7vNZLp+nY367Z76+zXRa846k5c6xu6pkzdTf6kPlvmU43aN+TRfpv5kBywPEwdyPygO/dSfLsCgkb170fuLrDFITQGhnftN3q+pzNupiTxGb1ZEoHfBKa/eTG6zaPQuyLapKV2jd3CRm1K8HaqiLPtTBXoXjKugQraFGr8Sk9TWwHavyq/EJuWGbG7U+SXl2C9M+JU4pTB22rH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PBa2AuYeW939OEnNr0TXLRjngnFlLlfJnYNIArwdqlDN+o2tKJHpgEkFz3XOimj0TnkK9SdAO48hdjaHapSTExg0sqAYU7aq6OzdYHnwm8ritQqqQYplPqXvgbvHfJpXRjE4oNecdSYcz8VpmFf5TgMBBu1+I2+tx0+b+cvi+OFYvh/Pu83TdP06azQfxMJj4z+fb57iLJbeqwAAAABJRU5ErkJggg=='); width:100px; height:100px; color:#000">Div 2</div>

希望这个例子对你有帮助

相关问题