JQuery:访问每个中的原始 jquery 对象
我正在尝试在 JQuery 中的一组 DIV 上实现 HTML 单选按钮行为。 我想从所有元素中删除“set”类,然后使用 addClass() 重新设置被单击的(一个)元素:
$(".button").each(function() {
$(this).click(function(){
// what goes here to call removeClass() on *all* the elements?
$(this).addClass("set");
});
});
我想对所有元素调用removeClass() - 在本例中为 $(".按钮”),但我无法明确引用 $(“.button”) 。
我不能只在循环外部调用 $(".button").removeClass("set") ,因为这是更大程序的一部分,并且each() 循环内的行为可以通过其他参数进行修改。
有没有办法从内部访问完整的元素集,或将它们作为变量传递? 或者还有其他方法来解决这个问题吗?
I'm trying to implement HTML radio button behaviour on a set of DIVs in JQuery. I want to remove the "set" class from all the elements then use addClass() to re-set the (one) element that is clicked:
$(".button").each(function() {
$(this).click(function(){
// what goes here to call removeClass() on *all* the elements?
$(this).addClass("set");
});
});
I want to call removeClass() on all the elements - in this case $(".button"), but I can't refer to $(".button") explicitly.
I can't just call $(".button").removeClass("set") outside the loop as this is part of a bigger program and the behaviour inside the each() loop can be modified by other parameters.
Is there any way to access the full set of elements from inside, or pass them in as a variable? Or is there another way to approach the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这看起来不必要地复杂,它可能有助于更清楚地了解您最终想要实现的目标。
在循环内,您可以使用 jquery $("div") // 在循环内获取您喜欢的任何内容
This seems needlessly complex, it might help to get a clearer picture of what you are ultimately trying to achieve.
Inside your loop you can fetch whatever you like with jquery $("div") // inside the loop
您可能正在寻找新的 选择器 功能,因为您正在制作插件:
You may be looking for the new selector feature, since you are making a plugin:
如果您无法在内部函数中对选择器进行硬编码,jQuery 实际上可以返回在原始调用中用作选择器的字符串。 请参阅 $('.bla').selector
这仅在较新版本中添加。
If you can't hard-code the selector in your inner functions, jQuery can actually return the string used as selector in the original call. See $('.bla').selector
This has been added only in the newer version though.
只是改变:
我不明白为什么这会是一个问题,每次都要拉 .button 有点慢,但这是像你想要的那样包含在循环中的唯一方法。
just change:
I dont see why that would be a problem, a bit slow having to pull .button each time, but that's the only way containing in a loop like you want to.
你说“但我不能明确引用 $(".button") 。”。 这是为什么?
You say "but I can't refer to $(".button") explicitly.". Why is that?
仍然不确定为什么不能从内部函数引用 $('.button') 但您可以捕获自由变量中的引用吗?
Still not sure why you cannot reference $('.button') from the inner function but can you capture the references in a free variable instead?
我回答了文本正文中提出的问题:如果您想设置同级的每个 other 元素的类,请使用siblings。
有趣,因为这是在 radioClass() 下的路线图中
你想要的是 < a href="http://docs.jquery.com/Traversing/siblings" rel="nofollow noreferrer">siblings,也不要使用each来设置点击事件。
请参阅此示例以了解上述操作:
http://jsbin.com/ewiqe
I answered the question asked in the body of the text: If you want to set the class of every other element that's a sibling, use siblings.
Interesting because this is in the roadmap under radioClass()
What you want is siblings, and don't use each to set the click event either.
See this example for the above in action:
http://jsbin.com/ewiqe