首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$(this).parent().siblings('.classname').show()不工作

$(this).parent().siblings('.classname').show()不工作
EN

Stack Overflow用户
提问于 2014-07-22 21:36:06
回答 2查看 546关注 0票数 0

HTML:

代码语言:javascript
复制
<div class="quiz-template">
    <span class="question">1. What is the first word in the first chapter of the series?</span>

    <div class="image-container">
        <img src="img2/q1.png" />
    </div>

    <div class="answer-container">
        <div class="a-choice1">
            <input type="radio" name="qa1" value="0" />
            <label for="btn1"></label>
            <span>The</span>

        </div>

        <div class="a-choice1">
            <input type="radio" name="qa1" value="0" />
            <label for="btn2"></label>
            <span>Albus</span>
        </div>

        <div class="a-choice1">
            <input type="radio" name="qa1" value="0" />
            <label for="btn3"></label>
            <span>Number</span>
        </div>

        <div class="a-choice1">
            <input type="radio" name="qa1" value="1" />
            <label for="btn4"></label>
            <span>Mr.</span>
        </div>
    </div>

    <div class="result-container" id="r1">
        <!--
        <span class="tic mark">✓</span> 
        <span class="cross mark">X</span>
        -->
        <span class="result-text"></span>
        <span class="result-description">
            The first sentence reads, “Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they were perfectly normal, thank you very much.”
        </span>
    </div>
</div>

我试图通过这个函数显示显示隐藏的结果容器类:none;

代码语言:javascript
复制
$('.a-choice1').click(function () {
    $(this).parent().siblings('.results-container').show("fast"); 
});

但不起作用。我的遍历代码有什么问题?另外,是否有一种方法可以使选择器内的值动态,这样我就可以执行一个循环?有点像:

代码语言:javascript
复制
$(this).parent().siblings('div[id=r '+N+']').show("fast");

其中N是一个整数,可以增加值以访问ids r1、r2、r3.诸若此类?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-22 21:50:41

试试这个:

代码语言:javascript
复制
$('.a-choice1').find("input[type=radio]").click(function () {
    $(this).parents(".answer-container").next(".result-container").show("fast");
});

parents() (带有选择器):http://api.jquery.com/parents/

next() (带有选择器):http://api.jquery.com/next/

这是给你的一把小提琴:

http://jsfiddle.net/rePgM/

要执行一个循环,应该如下所示:

代码语言:javascript
复制
$(this).parent().siblings('.result-container').each(function(e, i){
     $(this).show("fast");  //$(this) now refers to each instance of the parent's siblings
});

这也消除了您发送ID号的需要。

票数 0
EN

Stack Overflow用户

发布于 2014-07-22 21:39:44

尝试查找法

代码语言:javascript
复制
$(this).parent().find('div#r'+N).show("fast");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24898280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档