将类别添加到< body>如果一个DIV有多于4个元素 [英] Add Class to <body> if a DIV has more then 4 elements
问题描述
我试图在< div class =scroll> $ c $时向
< body>
c>有超过4个< li>文字< / li>
元素。
I'm trying to add class to <body>
when <div class="scroll">
has more then 4 <li>text</li>
elements.
HTML:
<body>
<div class="scroll">
<div> <!-- Parent Element -->
<ul> <!-- Parent Element 2 -->
<li>text</li> <!-- Child 1-->
<li>text</li> <!-- Child 2-->
<li>text</li> <!-- Child 3-->
<li>text</li> <!-- Child 4-->
<li>text</li> <!-- Child 5-->
</ul>
</div>
</div>
<body>
表示如果< div class =scroll>
有5 < li>文字< / li>
项目,然后将类别添加到正文
like < body classpopup>
。任何身体都知道如何通过 Jquery 来做到这一点。提前感谢。
Means if <div class="scroll">
has 5 <li>text</li>
item then add class to body
like <body class"popup">
. Any body know how to do this by Jquery. Thanks in advance.
推荐答案
您可以使用setInterval(只有在动态添加动态删除li时才可以使用,
You can use setInterval (only needed if your li's are added removed dynamically, you may skip it if it's not the case), and check it's length inside and do respective action as needed.
setInterval(function(){
if(jQuery('div.scroll ul > li').length > 4)
jQuery('body').addClass('popup');
else
jQuery('body').removeClass('popup');
}, 1000);
注意:您需要在关闭body标记之前或之后放置此脚本比较。
Note: You need to place this script before closing of body tag or after of elements for which length needs to be compared.
这篇关于将类别添加到< body>如果一个DIV有多于4个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!