2016-07-29 57 views
0

的子元素爲什麼這不起作用不能選擇一個特定的類

<div class='folder lev1'>323</div> 
<div class='folder lev2'>525</div> 
<div class='file lev3'>727</div> 
<div class='file lev3'>929</div> 
<div class='folder lev1'>end</div> 

JS

$(".lev1").click(function(){ 
    if ($(this).next().is(":visible")) { 
     $(this).nextUntil(".lev1").hide(); // works 
    } 
    else { 
     $(this).children(".lev2").show(); // doesn't work 
     $(this).find(".lev2").show(); // also tried - doesn't work 
     console.log("323"); // works 
    } 
}); 

控制檯是沒有錯誤。

+0

考慮我的更新答案 – Mojtaba

回答

2

在你的例子中,.lev1沒有任何chlidren。有兄弟姐妹。

$(".lev1").click(function(){ 
    if ($(this).next().is(":visible")) { 
     $(this).nextUntil(".lev1").hide(); // works 
    } 
    else { 
     $(this).siblings(".lev2").show(); // works 
    } 
}); 

或者,如果你想在未來.lev2顯示:

$(".lev1").click(function(){ 
    if ($(this).next().is(":visible")) { 
     $(this).nextUntil(".lev1").hide(); // works 
    } 
    else { 
     $(this).nextAll(".lev2").show(); // works 
    } 
}); 
+0

多麼愚蠢的錯誤。非常感謝。 – bonaca

+0

@bonaca,沒問題。 – Mojtaba