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:
我正在尝试在JQuery中的一组DIV上实现HTML单选按钮行为。我想从所有元素中删除“set”类,然后使用addClass()重新设置单击的(一个)元素:
$(".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.
我想在所有元素上调用removeClass() - 在本例中为$(“。button”),但我不能明确地引用$(“。button”)。
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.
我不能只在循环外调用$(“。button”)。removeClass(“set”),因为这是更大程序的一部分,而且每个()循环内部的行为可以被其他参数修改。
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?
有没有办法从内部访问完整的元素集,或作为变量传递它们?或者还有另一种解决问题的方法吗?
7 个解决方案
#1
3
You may be looking for the new selector feature, since you are making a plugin:
您可能正在寻找新的选择器功能,因为您正在制作插件:
jQuery.fn.test = function() {
console.log(this.selector); // .button
};
$('.button').test();
#2
2
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
如果你不能在内部函数中硬编码选择器,jQuery实际上可以返回在原始调用中用作选择器的字符串。请参阅$('。bla')。选择器
This has been added only in the newer version though.
这只是在新版本中添加的。
#3
0
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
在你的循环中,你可以使用jquery $(“div”)//在循环内获取你喜欢的任何内容
#4
0
You say "but I can't refer to $(".button") explicitly.". Why is that?
你说“但我不能明确地引用$(”。button“)。”这是为什么?
$(".button").each(function() {
$(this).click(function(){
$('.button').removeClass('set'); // this should do it
$(this).addClass("set");
});
});
#5
0
just change:
只是改变:
$(".button").each(function() {
$(this).click(function(){
$('.button').removeClass("set");
$(this).addClass("set");
});
});
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有点慢,但这是包含在你想要的循环中的唯一方法。
#6
0
Still not sure why you cannot reference $('.button') from the inner function but can you capture the references in a free variable instead?
仍然不确定为什么你不能从内部函数引用$('。button'),但是你可以在*变量中捕获引用吗?
var buttons = $(".button");
buttons.each(function() {
$(this).click(function(){
// what goes here to call removeClass() on *all* the elements?
buttons.removeClass('set');
$(this).addClass("set");
});
});
#7
0
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()
有意思,因为这是在radioClass()下的路线图中
What you want is siblings, and don't use each to set the click event either.
你想要的是兄弟姐妹,也不要用它们来设置点击事件。
$(".button").click(function() {
$(this).addClass('set');
$(this).siblings('.button').removeClass('set');
});
See this example for the above in action:
有关上述操作,请参阅此示例:
http://jsbin.com/ewiqe
#1
3
You may be looking for the new selector feature, since you are making a plugin:
您可能正在寻找新的选择器功能,因为您正在制作插件:
jQuery.fn.test = function() {
console.log(this.selector); // .button
};
$('.button').test();
#2
2
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
如果你不能在内部函数中硬编码选择器,jQuery实际上可以返回在原始调用中用作选择器的字符串。请参阅$('。bla')。选择器
This has been added only in the newer version though.
这只是在新版本中添加的。
#3
0
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
在你的循环中,你可以使用jquery $(“div”)//在循环内获取你喜欢的任何内容
#4
0
You say "but I can't refer to $(".button") explicitly.". Why is that?
你说“但我不能明确地引用$(”。button“)。”这是为什么?
$(".button").each(function() {
$(this).click(function(){
$('.button').removeClass('set'); // this should do it
$(this).addClass("set");
});
});
#5
0
just change:
只是改变:
$(".button").each(function() {
$(this).click(function(){
$('.button').removeClass("set");
$(this).addClass("set");
});
});
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有点慢,但这是包含在你想要的循环中的唯一方法。
#6
0
Still not sure why you cannot reference $('.button') from the inner function but can you capture the references in a free variable instead?
仍然不确定为什么你不能从内部函数引用$('。button'),但是你可以在*变量中捕获引用吗?
var buttons = $(".button");
buttons.each(function() {
$(this).click(function(){
// what goes here to call removeClass() on *all* the elements?
buttons.removeClass('set');
$(this).addClass("set");
});
});
#7
0
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()
有意思,因为这是在radioClass()下的路线图中
What you want is siblings, and don't use each to set the click event either.
你想要的是兄弟姐妹,也不要用它们来设置点击事件。
$(".button").click(function() {
$(this).addClass('set');
$(this).siblings('.button').removeClass('set');
});
See this example for the above in action:
有关上述操作,请参阅此示例:
http://jsbin.com/ewiqe