我有一個列表,其結(jié)構(gòu)如下:<ul data-Id="2" class="listElement"><li></li><li></li><li> <ul data-Id="3" class="listElement" style="display: none"> <li></li> <li> <ul data-Id="4" class="listElement" style="display: none"> <li></li> </ul> </li> </ul></li>我需要能夠定位當(dāng)前隱藏的 ul 元素,并slidetoggle()對其執(zhí)行操作。我嘗試在 jQuery 中做到這一點(diǎn),但效果不太好:var test = 3; //i need to declare it as a var, because i get this from another item $('.listElement').find('[data-Id="' + test + '"]').slideToggle("fast");什么都沒發(fā)生,我似乎找不到我在這里錯過的東西
1 回答

尚方寶劍之說
TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個贊
您的代碼是正確的,但請確保當(dāng)您的 javascript 代碼運(yùn)行時 dom 元素已經(jīng)放置(準(zhǔn)備好)?;蛘吣?javascript 應(yīng)該在雇用的文檔準(zhǔn)備就緒時運(yùn)行。
$(document).ready(function(){ var test = 3; //i need to declare it as a var, because i get this from another item $('.listElement').find('[data-Id="' + test + '"]').slideToggle("slow"); });
- 1 回答
- 0 關(guān)注
- 106 瀏覽
添加回答
舉報
0/150
提交
取消