为孩子添加类 - jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11347033/
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
add class to children - jquery
提问by miszczu
I have this html code:
我有这个 html 代码:
<div class="rotation">
<ul>
<li><img src="..." alt="" /></li>
<li><img src="..." alt="" /></li>
<li><img src="..." alt="" /></li>
</ul>
</div>
I need to add class to ul, i want to do it using jquery, because ul, li are generated without any class.
Let say ul tag need to be <ul class="image_rotation">
. How can i do it?
我需要向ul添加类,我想使用jquery来做,因为ul,li是在没有任何类的情况下生成的。
假设 ul 标签需要是<ul class="image_rotation">
. 我该怎么做?
回答by lbstr
Read about jQuery's addClass
method here.
$('.rotation ul').addClass('image_rotation');
The above code will add a class of image_rotation
to every ul
that is a descendant of any element with the class rotation
.
上面的代码将向image_rotation
每个ul
元素添加一个类,该类是具有该类的任何元素的后代rotation
。
If you want to target only ul
s that are an immediate childof an element with a class of rotation
, use the child selector (>
):
如果您只想定位属于类为 的元素ul
的直接子元素的s rotation
,请使用子选择器 ( >
):
$('.rotation > ul').addClass('image_rotation');
If you only want to do this on one .rotation
element, you should give it an id and select it that way. For example:
如果您只想在一个.rotation
元素上执行此操作,您应该给它一个 id 并以这种方式选择它。例如:
$('#rotation ul');
and
和
<div id="rotation" class="rotation">
<ul>
<li><img src="..." alt="" /></li>
<li><img src="..." alt="" /></li>
<li><img src="..." alt="" /></li>
</ul>
</div>
回答by David says reinstate Monica
I'd suggest keeping it simple:
我建议保持简单:
$('div.rotation ul').addClass('image_rotation');
Or, if you have multiple ul
elements within the div.rotation
element, you can specify the first ul
:
或者,如果ul
元素中有多个元素div.rotation
,则可以指定第一个ul
:
$('div.rotation ul:first').addClass('image_rotation');
The last ul
:
最后ul
:
$('div.rotation ul:last').addClass('image_rotation');
Or a ul
of arbitray zero-based index, for example the third ul
(with an index of 2
):
或者一个ul
从零开始的任意索引,例如第三个ul
(索引为2
):
$('div.rotation ul:eq(2)').addClass('image_rotation');
or:
或者:
$('div.rotation ul').eq(2).addClass('image_rotation');
Or an immediate child (so no ul
elements nested in any other elements):
或者直接子ul
元素(因此没有元素嵌套在任何其他元素中):
$('div.rotation > ul').addClass('image_rotation');
References:
参考:
回答by Nir
all you need is to find the UL in the div and assign it your class. you can see it in this demo, but the code for your HTML is below
您所需要的只是在 div 中找到 UL 并将其分配给您的班级。你可以在这个演示中看到它,但你的 HTML 代码在下面
$(document).ready(function(){
$('ul','div.rotation').each(function(index){
$(this).addClass('image_rotation');
});
});?
回答by Karesh A
$(".rotation ul").attr("class","image_rotation");
回答by exaptis
It's quite easy, just do:
这很简单,只需执行以下操作:
$(document).ready(function(){
$('div.rotation ul').addClass('image_rotation');
});
回答by Pranay Rana
$(".rotation").children("ul").addClass( "MyClass");