jQuery selector with variable

家住魔仙堡 提交于 2019-12-12 03:49:33

问题


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


回答1:


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+'"]')



回答2:


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);
    });


来源:https://stackoverflow.com/questions/4108845/jquery-selector-with-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!