2009-09-26 44 views
0

在以下javascript/html中,我可以参考div.answer但我该如何参考div#flashcard-001.answer如何在jQuery中引用带id + class的HTML元素?

HTML:

<div id="flashcard-001" class="flashcard"> 
    <div class="question">What color is the sky?</div> 
    <div class="answer">blue</div> 
    <button class="show">Show</button> 
    <button class="hide">Hide</button> 
</div> 

的Javascript:

//run when page is loaded 
google.setOnLoadCallback(function() { 
    $("div.answer").hide(); //WORKS 
    $("div#flashcard-001.answer").hide(); //DOES NOT WORK 
    $("button.show").bind("click", function(e) { 
     $("div.answer").show(); 
    }); 
    $("button.hide").bind("click", function(e) { 
     $("div.answer").hide(); 
    }); 
}); 

回答

10

你缺少空间:

$("div#flashcard-001 .answer").hide(); 
+0

曾任职我,谢谢。 – 2013-03-06 10:58:28

0

最外面的div有一个完整的选择:

div#flaschard-001.flashcard 

不是:

div#flashcard-001.answer 

我相信你只是使用了错误的选择。

3

尝试:。

$( “#烧录卡-001”),儿童( “答”)隐藏();

+1

+1此解决方案比'$(“div#flashcard-001 .answer”)'或'$(“#flashcard-001 .answer”)更有效率。 – Gumbo 2009-09-26 19:08:25

+0

@Jourkey:不,它的确是因为jQuery从右向左评估选择器。 – Gumbo 2009-09-26 19:13:37

2

既然你要选择的DIV元素的后代与ID 烧录卡-001,你需要的descendant selector或 - 因为你的元素也是一个直接的孩子 - 的child selector

div#flashcard-001 .answer 
div#flashcard-001 > .answer 
相关问题