jQuery selector with variable

2019-09-06 08:00发布

I need to select all images whose filename contain a certain string (variable) using jQuery. I am using:
var str='-out.';
$('img[src*='+str+']'). //do something

It works but fires the following warning in Firefox: " Expected ']' to terminate attribute selector but found '.' " Would someone know how to avoid this warning ?

Thank You

2条回答
可以哭但决不认输i
2楼-- · 2019-09-06 08:43

I think you need double quotes or single around the attribute value.

Look at Jquery Docs for attr*="value" selector

like this:

$("img[src*='"+str+"']")

OR

$('img[src*="'+str+'"]')
查看更多
在下西门庆
3楼-- · 2019-09-06 08:49

I try the following code and firefox doesn't fires any warning (it works as aspected)?

   var str=".png";
    $('img[src*='+str+']').each(function(){
      console.info(this.alt);
    });
查看更多
登录 后发表回答