2
我想檢索多個級別的子元素的類名稱。我正在嘗試跟隨,但它只給出了後方子元素的類名。我究竟做錯了什麼 ?如何檢索jquery中多個級別的所有子元素的類名?
<script type="text/javascript">
$(document).ready(function(){
thisP=$("#myParagraph");
getChildStyles(thisP);
//function
function getChildStyles(thisobj) {
var classNames;
var classNames1;
$(thisobj).children().each(function(){
classNames+=$(this).attr('class');
if($(this).children().length>0) {
classNames1+=getChildStyles($(this));
}
classNames+=classNames1;
});
return classNames;
}
});
</script>
和HTML,
<ul id="myParagraph" class"mainUL">
<li id="LIOne">ksjdfhsdf</li>
<li id="LITwo">skdjfkdsf<span class"span1Class"><span class="span2class"></span>
</span></li>
<li id="LIThree" class="thirdLIClass">edroiutret</li>
</ul>