为孩子添加类 - 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:30:14  来源:igfitidea点击:

add class to children - jquery

jqueryhtml

提问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 addClassmethod here.

addClass在这里阅读 jQuery 的方法。

$('.rotation ul').addClass('image_rotation');

The above code will add a class of image_rotationto every ulthat is a descendant of any element with the class rotation.

上面的代码将向image_rotation每个ul元素添加一个类,该类是具有该类的任何元素的后代rotation

If you want to target only uls 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 .rotationelement, 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 ulelements within the div.rotationelement, 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 ulof 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 ulelements 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");