jQuery 语法:使用变量来定位 id
我正在尝试执行以下操作,但始终失败:
我有一个变量:
var targg = "new-house";
并且我想使用该变量的值来定位具有该 id 的元素:
但是我试图在一组参数内执行此操作,但它不起作用。
这是最终的代码:
var targg = "new-house";
$(this).someFunction({
para1: true,
para2: "string",
para3: "#"+targg
});
然而,这有效:
$(this).someFunction({
para1: true,
para2: "string",
para3: "#new-house"
});
这可能很简单,但我找不到它! 希望它是清楚的。 谢谢
I'm trying to do the following but keep failing:
I have a variable:
var targg = "new-house";
And I would like to use the value of the variable to target the element with that id:
However I'm trying to do that inside a set of parameters and it does not work.
Here is the final Code:
var targg = "new-house";
$(this).someFunction({
para1: true,
para2: "string",
para3: "#"+targg
});
This however works:
$(this).someFunction({
para1: true,
para2: "string",
para3: "#new-house"
});
It is probably very easy but I cannot find it!!
Hope that it is clear.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这
针对的是正确的选择器。
要么您没有具有此类 id 的元素,要么您没有在 someFunc() 中正确调用它。
查看相关小提琴:
This
targets the proper selector.
Either you don't have an element with such an id, or you aren't calling it correctly in your someFunc().
See relevant fiddle: