如何使用Jquery检查属性的值是否为图像源?

时间:2021-11-23 20:32:03

I would like to create a condition that checks the attribute for a particular value and the source of an image. If both values both match true, then do something.

我想创建一个条件来检查特定值的属性和图像的来源。如果两个值都匹配为true,则执行某些操作。

So far I have:

到目前为止我有:

if ($('meta[content="New Reviews"]').length > 0) && ($( "img[src*='default-label']" ) {

  //Do something

}

Not sure if this is written correctly but I believe I wrote the syntax wrong.

不确定这是否写得正确,但我相信我写的语法错了。

1 个解决方案

#1


2  

You should make sure your parentheses match, and that you check length for both collections:

您应该确保您的括号匹配,并检查两个集合的长度:

if ($('meta[content="New Reviews"]').length && $('img[src*="default-label"]').length) {
  console.log('Found!')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta content="New Reviews" />
<img src="default-label/image.png" />

#1


2  

You should make sure your parentheses match, and that you check length for both collections:

您应该确保您的括号匹配,并检查两个集合的长度:

if ($('meta[content="New Reviews"]').length && $('img[src*="default-label"]').length) {
  console.log('Found!')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta content="New Reviews" />
<img src="default-label/image.png" />