jQuery:如何使用变量作为选择器 [英] Jquery:How to use variable as a selector
本文介绍了jQuery:如何使用变量作为选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图弄清楚如何在选择器中使用变量.如果我在选择器中使用ID的实际值,则效果很好.
I trying to figure out how can I use a variable inside a selector. If I use the actual value of the ID in the selector, it works very well.
$(document).on("click",'.tag', function(){
var data-id = $(this).attr('data-id');
if($('div[data-id="540"]').hasClass("tag_current")) {
$('div[data-id="540"]').removeClass("tag_current").addClass("tag");
}
$(this).toggleClass('tag tag_current');
});
我检查了很多答案并以不同的方式尝试,但是我仍然没有解决.
I checked many answers and try it in different ways, but I still didn't solve it.
推荐答案
基本字符串串联:
$('div[data-id=' + data-id + ']')
这篇关于jQuery:如何使用变量作为选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文