[英]jQuery. How do I combine a JS variable with a tag literal?
Here I have a modal dialog that hosts two tabs: http://www.bootply.com/f24gE2KvF3 在这里,我有一个模式对话框,其中包含两个选项卡: http : //www.bootply.com/f24gE2KvF3
In the JS code: 在JS代码中:
$('#trackFilterTabSet_GUID a').click(function (e) {
// present tab
alert('present tab');
e.preventDefault();
$(this).tab('show');
})
$('#trackFilterTabSet_GUID a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
// do something awesome
// alert('shown.bs.tab event fired');
e.target // activated tab
e.relatedTarget // previous tab
})
I want to replace #trackFilterTabSet_GUID
with a JS variable: 我想用JS变量替换
#trackFilterTabSet_GUID
:
var mySelector = "#trackFilterTabSet_" + myStringGeneratingFunction();
How do I combine this JS variable with a
in the first function and a[data-toggle="tab"]
in the second function? 如何合并这个JS变量,
a
在第一功能和a[data-toggle="tab"]
在第二个功能?
Exactly as you'd expect: 完全符合您的期望:
var mySelector = "#trackFilterTabSet_" + myStringGeneratingFunction();
$(mySelector + ' a').click(function (e) {...
$(mySelector + ' a[data-toggle="tab"]').on(...
A jQuery selector is simply a string
. jQuery选择器只是一个
string
。 String concatenation works perfectly fine with it. 字符串连接可以很好地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.