我有一個主要的div容器,裏面有4個隱藏的div(我在css中用display:none創建了一個類,並將它添加到每個內部div),主div讀取從下拉列表中取值並根據讀取的值取消隱藏多個div。removeClass方法不工作jquery
<div id="ddlContainer">
<div id="div1" class="hide">
<center>Select option:</center>
<select id="ddl1">
<!--options are added here-->
</select>
</div>
<div id="div2" class="hide">
<center>Select option:</center>
<select id="ddl2">
<!--options are added here-->
</select>
</div>
<div id="div3" class="hide">
<center>Select option:</center>
<select id="ddl3">
<!--options are added here-->
</select>
</div>
<div id="div4" class="hide">
<center>Select option:</center>
<select id="ddl4">
<!--options are added here-->
</select>
</div>
</div>
但removeClass不適用於我。
var diff, NumofHiddenDDLs = $("#ddlContainer").children().filter("[class=hide]");
if (ReadValue > NumofHiddenDDLs.length) {
diff = 1;
}
else {
diff = 2;
}
if (diff == 1) {
//TODO add class hide to shown divs
}
else {
for (var i = 0; i < ReadValue; i++)
($("#ddlContainer").children()[i]).removeClass("hide");
}
有誰能告訴我我在做什麼錯嗎? 感謝
只需使用'var diff = $('#ddlContainer> .hide')。length; if(diff === 1){TODO} else {$('#ddlContainer> .hide')。removeClass('hide')}' – Tushar