1

我有一个问答列表,顶部有“打开所有/关闭所有”,单击时会切换单独的打开和关闭图像按钮。这很好用。

然后跟随个人问答,每个问答都有自己的开放和关闭形象。

如果您在页面加载后首先单击“全部打开/全部关闭”,然后单击个别问答打开/关闭图像,一切正常。但是,如果在页面加载后您单击个别问答打开/关闭图像,绕过“打开所有/关闭所有”,它们会显示不适当的打开或关闭图像。

这是页面代码:

<div class="answersee"><span>Open All</span><img src="assets/open.gif" border="0" alt="" /></div>
<div class="answerhide"><span>Close All</span><img src="assets/close.gif" border="0" alt="" /></div>

<div class="qa">
    <div><img src="open.gif" border="0" alt="" /><span class="question">Question.</span></div>
    <div class="answer"><p>Answer.</p></div>
</div>

这是脚本(也使用 Jquery):

$(function () {
    $(".qa").click(function () {
        $(this).find("div").next().slideToggle("fast");
        if ($(this).find("div:eq(0)").find("img").attr("src") == "open.gif") {
            $(this).find("div:eq(0)").find("img").attr("src", "close.gif");
        }
        else {
            $(this).find("div:eq(0)").find("img").attr("src", "open.gif");
        }
    });
    $(".answersee").click(function () {
        $(".answer").show("fast");
        $(".qa > div > img").attr("src", "close.gif");
        $(".answerhide").show();
        $(".answersee").hide();
    })
    $(".answerhide").click(function () {
        $(".answer").hide("fast");
        $(".qa > div > img").attr("src", "open.gif");
        $(".answersee").show();
        $(".answerhide").hide();
    })
});

我不认为这是 CSS 问题,或者我会在此处包含该代码。我需要以某种方式初始化脚本吗?还是我在上面的脚本中犯了错误?

4

4 回答 4

1

这就是我将如何做到的。

工作演示 →

编辑:

更新代码以具有简单的打开/关闭链接。

带有注释的代码解释了我的方法:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<style>
    body
    {
        font-family: "Verdana";
        font-size: 12px;

    }
    .question
    {
        background-color: #ccc;
        cursor: pointer;
        padding: 5px;
        margin-bottom: 10px;
        border-bottom: 1px solid #000;
    }

    .answer {
        padding: 5px;
    }


</style>

<script>



    $(document).ready(
        function()
        {
            //Hide all the answers on page load.
            $('.answer').hide();

            //For all questions, add 'open'/'close' text.
            //You can replace it with an image if you like. 
            //This way, you don't need to specify img tag in your HTML for each question.
            $('.question')
                .append(' <span>[ open ]</span>');


            //Now there are two ways to toggle the visibility of answer.
            //Either click on the question OR click on Open All / Close All link.
            //To use the same code for both instances, we will create
            //a function which will take the 'question' div and toggle the answer for it.
            //Advantage of this approach is that the code to toggle the answer is in
            //one place.

            //By default, this function will try to toggle the status of the answer
            //i.e. if it's visible, hide it otherwise show it.
            //This function will take a second argument called 'showAnswer'.
            //If this argument is passed, it overrides the toggle behavior.
            //If 'showAnswer' is true, answer is shown.
            //If it's false, answer is hidden.
            //This second parameter will be used by the 'openAll', 'closeAll' links.
            var toggleAnswer = function toggleAnswer(question, showAnswer)
            {
                //The way I have structured the HTML, answer DIV is right after 
                //question DIV.
                var $answer = $(question).next('div');

                //Animation callback, after the animation is done, we want to 
                //switch the 'text' to display what could the user do with the question.
                //Once again, you can change this code to show open/close image.
                var updateText = function()
                                 {
                                    var text = $answer.is(':visible') ? ' [close] ' : ' [open] ';
                                    $(question).find('span').html(text);
                                 }

                var method = null;

                if(arguments.length > 1)
                {
                    //If the function was called with two arguments, use the second
                    //argument to decide whether to show or hide.
                    method = showAnswer === true ? 'show' : 'hide';
                }
                else
                {
                    //Second argument was not passed, simply toggle the answer.
                    method = $answer.is(':visible') ? 'hide' : 'show';
                }

                $answer[method]('fast', updateText);
            };

            //On each question click, toggle the answer. 
            //If you have noticed, I didn't enclose both Q&A inside one DIV.
            //The way you have done if user clicks on the answer, answer will collapse.
            //This may not be desirable as user may want to copy the answer
            //and he won't be able to.
            $('.question').click(function(){ toggleAnswer(this);});

            //We will reuse the same toggleAnswer method in openAll, closeAll 
            //handling. This way, if you want to change behavior of how the question/answers
            //are toggled, you can do it in one place.
            $('#openClose').click(
                function() 
                { 
                    var showAnswer = $(this).html().toLowerCase().indexOf('open') != -1 ? true : false;
                    $('.question').each(function() { toggleAnswer(this, showAnswer); });
                    $(this).html(showAnswer ? 'Close All' : 'Open All'); 
                    return false;
                } 
            );

        }
     );
</script>
<html>
<head>
<title>simple document</title>
</head>
<body>

<a id='openClose' href='#'>Open All</a>

<br /><br />

<div class='question'>Question 1</div>
<div class='answer'>Answer 1</div>

<div class='question'>Question 2</div>
<div class='answer'>Answer 2</div>

<div class='question'>Question 3</div>
<div class='answer'>Answer 3</div>

</body>
</html>
于 2009-07-31T04:56:35.593 回答
0

Redsquare 和解决方案瑜伽士:

谢谢。我稍后会再次回复并发布一个工作演示,以便您更清楚地看到问题。对不起,我以前应该这样做的。

丽兹

于 2009-07-31T15:03:11.480 回答
0

感谢您抽出宝贵时间提供此信息。我将在今天晚些时候尝试这个并报告回来。在我的版本中,我切换了 Open All/Close All 功能。由于您无需移动鼠标,因此外观更简洁且更易于使用。

于 2009-07-31T14:58:41.270 回答
0

您需要使用回调,因为您的动画在检查正在显示的图像时还没有完成。

 $(".qa").click(function() {
    $(this).find("div").next().slideToggle("fast", toggleImage);
 }

 function toggleImage(){
    var $img = $(this).find("img");
    $img.attr('src') == "open.gif" ? $img.attr('src', "close.gif") : $img.attr('src', "open.gif");

 }

注意有更好的方法可以做到这一点,但让你先工作,然后看看你是否想再重构它。

于 2009-07-31T00:16:41.393 回答