带有变量的jQuery选择器 [英] jQuery selector with variable

查看:98
本文介绍了带有变量的jQuery选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用jQuery选择文件名包含特定字符串(变量)的所有图像.我正在使用:
var str='-out.';
$('img[src*='+str+']'). //do something

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

它可以工作,但是会在Firefox中引发以下警告:预期']'终止属性选择器,但找到'.'. 有人知道如何避免这种警告吗?

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

谢谢

推荐答案

我认为您需要在属性值两边加上双引号或单引号.

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

查看Jquery文档中的 attr * ="value" 选择器

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

像这样:

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

OR

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

这篇关于带有变量的jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆