如何在jQuery中引用具有id + class的HTML元素? [英] How to a reference an HTML element with id+class in jQuery?

查看:298
本文介绍了如何在jQuery中引用具有id + class的HTML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下javascript/html中,我可以引用 div.answer ,但是如何引用 div#flashcard-001.answer ?

In the following javascript/html, I can reference div.answer but how do I reference div#flashcard-001.answer?

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();
    });
});

推荐答案

您缺少空格:

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

这篇关于如何在jQuery中引用具有id + class的HTML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆