简体   繁体   中英

applying class to parent if child has it using jQuery

How can I use jQuery when I have this HTML code I need to add the class name to the parent li only when the child has the class name high.

thanks.

<li class="emergency" style="background-color: #FE9D9D;">
<span class="high">250</span></li>

您可以像这样定位.high类,然后使用close()获取父级li。

$('li .high').closest('li').addClass('myClass');

Use .has()

$('li:has("span.high")').addClass('className');

li has span element with class high addCass className to it.

.addclass()

Fiddle Demo

You can use has() :

$('li:has("span.high")').addClass('newClass');

Fiddle Demo

$(".high").parent().addClass('sss');

jsfiddle http://jsfiddle.net/M5gNA/

如果我正确理解了这个问题,您也想将span的类也添加到父li ,对吗?

$('li .high').closest('li').addClass('high');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM