jQuery .addclass 到多个 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19128594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .addclass to multiple div
提问by Ryan Parker
I am trying to apply the class ".lightGray" that i defined earlier on to div 4,5,7,and this current one. Not sure what I am doing wrong!
我正在尝试将我之前定义的类“.lightGray”应用到 div 4,5,7 和当前的 div。不知道我做错了什么!
$("#Div8").click(function(){$("#Div4", "#Div5","#Div7","#Div8").addclass(".lightGray");});
回答by Tushar Gupta - curioustushar
Use .addClass("lightGray");
as .addClass()takes className not .className
使用.addClass("lightGray");
如.addClass()需要的className不是.className
and change
和改变
$("#Div4,#Div5,#Div7,#Div8")
to
到
$("#Div4 ,#Div5, #Div7 ,#Div8")
查看多重选择器
you code becomes
你的代码变成
$("#Div8").click(function(){
$("#Div4 ,#Div5 ,#Div7 ,#Div8").addClass("lightGray");
^ //remove dot from here
});
回答by Scary Wombat
when you add a class you do not need the dot.
当你添加一个类时,你不需要点。
try this instead
试试这个
$("#Div8").click(function(){$("#Div4, #Div5, #Div7, #Div8").addClass("lightGray");});
回答by Guffa
You have made four separate selectors instead on one selector.
您在一个选择器上制作了四个单独的选择器。
The commas should be inside the string:
逗号应该在字符串内:
$("#Div4,#Div5,#Div7,#Div8")
The method name is addClass
, not addclass
, and there shouldn't be a period before the class name:
方法名是addClass
,不是addclass
,并且类名前不应该有句点:
$("#Div8").click(function(){$("#Div4,#Div5,#Div7,#Div8").addClass("lightGray");});
回答by sudhAnsu63
Try this.
尝试这个。
$("#Div8").click(function(){
$("#Div4,#Div5,#Div7,#Div8").addClass("lightGray"); //removed exra quotes from selecter
//removed dot(.) from class name
//Changed *addclass* to **addClass**
});
回答by Walter Gandarella
In all DIVS these I put a default class: changeClass
, and maintain their individual IDS.
在所有 DIVS 中,我放置了一个默认类:changeClass
,并维护它们各自的 IDS。
A instead of doing $("#Div4","#Div5","#Div7","#Div8")
would suffice to make $(".changeClass).addClass("lightGray");
A 而不是 do$("#Div4","#Div5","#Div7","#Div8")
就足以使$(".changeClass).addClass("lightGray");
Classes can be used in more than one element at the same time and thus can manipulate several of these elements at once, saving time and lines of code.
类可以同时用于多个元素,因此可以同时操作多个元素,从而节省时间和代码行。