2013-04-04 106 views
3

我使用下面的HTML代碼:選擇元素的所有子元素並使用jQuery淡出它們?

<!DOCTYPE html> 
<html> 
<head> 
    <title>Project Quiz</title> 
    <link rel="stylesheet" type="text/css" href="z/baseCss.CSS"> 
    <script src="/jquery-1.9.1.min.js"></script> 
    <script src="/baseJS.js"></script> 
</head> 
<body> 

<div id=header></div> 
<div id=contain> 
    <h1>Welcome to my web application</br> 
    Please enter your name, click 'continue' and have fun</h1> 
    <form> 
     <input type="text" id="name" value="John Doe"/> 
    </form> 
    <div class="awesome">Continue</div><br/> 
</div> 
<div id=footer></div> 

</body> 
</html> 

和jQuery的代碼:

$(document).ready(function(){ 
    $("input") 
     .focus(function(){ 
     $(this).css('outline-color','#559FFF'); 
     $(this).blur(function(){ 
      $(this).css("outline-color","#FF0000"); 
     }); 
    }); 
    $("input").click(function(){ 
    var value = $(this).val(function(){ 
     $(this).html(""); 
     }); 
    }); 
    $(".awesome").click(function(){ 
     b._slide(1000); 
    }); 
    var b = $("div:nth-child(2)"); 
    alert(b); 
}); 

我的問題是,我無法弄清楚如何選擇的<div id="contain">,只是所有的孩子讓他們淡出,當我點擊我的div按鈕,這是與「真棒」類的人。

這是我到目前爲止已經試過:這裏

$(".contain").each(function(){ 
    $(this).fadeOut(1000); 
}); 

but it didnt work also i tried: 
$(".contain").children(function(){ 
    $(this).fadeOut(1000); 
}); 

相同的結果。

我在做什麼錯?我只需要fadeOut<div id="contain">的內容,並保持其他所有內容相同。

回答

3

您需要使用:

$("#contain").children().fadeOut(1000); 

你嘗試錯誤的原因是:

$(".contain").each(function(){ $(this).fadeOut(1000); }); 

選擇所有.contain類的元素,並隱藏他們

$(".contain").children(function(){ $(this).fadeOut(1000); }); 

選擇與.contain類的元素,然後你傳遞一個函數來.children()它不處理。

請注意,在你的情況下,contain是一個ID,而不是一個類。

+0

感謝我unerstand它!非常感謝! – 2013-04-04 23:01:45

+0

我很高興我的幫助,祝你好運 שמזשעזרתי,בהצלחה:) 如果這個答案解決了你的問題,請考慮接受它。 – 2013-04-04 23:03:40

1

旁邊標有「。」。以「#」形成jQuery選擇,如果你不需要插入其他任何東西或顯示新的內容到<div id="contain">,你可以做到這一點

$("#contain").fade(1000); 

所有的孩子現在也淡出