带有变量的 jQuery 选择器 [英] jQuery selectors with variables
本文介绍了带有变量的 jQuery 选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将变量与选择器混合使用?
How to mix variables with selectors?
我有 ID 变量.
我想从 div #one 中选择具有此 ID 的图像.
I want to select image with this id from div #one.
jQuery('#one img .id')
是选择器.我试过 $('#one img .'+id)
但没有用.
jQuery('#one img .id')
is the selector. I've tried $('#one img .'+id)
but doesn't work.
推荐答案
根据您在下面的评论,您可以使用:
Based on your comment below, you would use this:
$('#one img.'+id)
在您的问题中,img
和 .class
之间有一个空格,我只是删除了它,所以您得到 img.className
或 img.'+className
In your question you have a space between img
and the .class
, I've simply removed that so you get img.className
or img.'+className
随着模板文字 在 ECMAScript 2015 中,你也可以这样做
With the introduction of template literals in ECMAScript 2015, you can also do
$(`#one img.${id}`)
这篇关于带有变量的 jQuery 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文