[英]jQuery add class to children of a tag and children of children of all the tags
I need to add a class to all the following tags. 我需要为以下所有标签添加一个类。 The sample JS I have included only adds the class to the direct children tags
我包括的示例JS仅将类添加到直接子标签中
before 之前
<div name='myDiv'>
<fieldset>
<div><input>exmaple</input></input></div>
</fieldset>
</div>
jQuery: jQuery的:
$("[name='myDiv']").children().addClass("myClass");
after 后
<div name='myDiv'>
<fieldset class="myClass">
<div><input>exmaple</input></input></div>
</fieldset>
</div>
However, the following is the expected result: 但是,以下是预期结果:
<div name='myDiv'>
<fieldset class="myClass">
<div class="myClass"><inputclass="myClass">exmaple</input></input></div>
</fieldset>
</div>
use *
. 使用
*
。 see snippet below 请参阅下面的代码段
PS input tags do not need to be closed with </input>
. PS输入标签不需要用
</input>
关闭。 you can add a label associated to them if you want 您可以根据需要添加与其关联的标签
$("[name='myDiv'] *").addClass("myClass");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div name='myDiv'> <fieldset> <div><input>exmaple</div> </fieldset> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.