鼠标悬停,mouseEnter

时间:2017-07-06 07:49:28

标签: jquery html

我有一个FAQ和问题页面。如果用户将鼠标悬停在某个问题上,则会显示答案。

但如果您现在将鼠标悬停在一个问题上,则会显示所有答案。什么不是必须的。只有所选问题的答案必须可见。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>A One Page Faq</title>
    <link href="Content/site.css" rel="stylesheet">
    <script src="Scripts/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $('.answer').hide();
            var $answer = $(this).next('.answer');
            $('.main h2').mouseover(function () {
                $('.answer').show();
                $answer.slideDown();
                $(this).addClass('close');
            });
            $('.main h2').mouseout(function () {
                $('.answer').hide();
                $answer.fadeOut();
                 $(this).removeClass('close');
            });           

        }); // end ready
    </script>
</head>
<body>
    <div class="wrapper">
        <div class="content">
            <div class="main">
                <h1>A One Page FAQ</h1>
                <div class="faq">
                    <h2>I've heard that JavaScript is the long-lost fountain of youth. Is this true?</h2>
                    <div class="answer">
                        <p>Why, yes it is! Studies prove that learning JavaScript freshens the mind and extends life span by several hundred years. (Note: some scientists disagree with these claims.)</p>
                    </div>
                </div>
                <div class="faq">
                    <h2>Can JavaScript really solve all of my problems?</h2>
                    <div class="answer">
                        <p>Why, yes it can! It's the most versatile programming language ever created and is trained to provide financial management advice, life-saving CPR, and even to take care of household pets.</p>
                    </div>
                </div>
                <div class="faq">
                    <h2>Is there nothing JavaScript <em>can&#8217;t</em> do?</h2>
                    <div class="answer">
                        <p>Why, no there isn&#8217;t! It&#8217;s even able to write its own public relations-oriented Frequently Asked Questions pages. Now that&#8217;s one smart programming language!</p>
                    </div>
                </div>
            </div>
        </div>
        <footer>
            <p>JavaScript &amp; jQuery: The Missing Manual, 3rd Edition, by <a href="http://sawmac.com/">David McFarland</a>. Published by <a href="http://oreilly.com/">O'Reilly Media, Inc</a>.</p>
        </footer>
    </div>
</body>
</html>

谢谢

如果我这样做:

 $(document).ready(function () {
            $('.answer').hide();
            var $answer = $(this).next('.answer');


            $('.main h2').mouseover(function (e) {
                $('.answer').hide();//Hide All Other Answers
                $(e).closest('.answer').show();//Show Closest Answer To Question
            });


            $('.main h2').mouseout(function (e) {
                $('.answer').hide();
            });

没有任何反应

5 个答案:

答案 0 :(得分:2)

替换

 $('.main h2').mouseover(function () {
     $('.answer').show();
     $answer.slideDown();
     $(this).addClass('close');
 });
 $('.main h2').mouseout(function () {
     $('.answer').hide();
     $answer.fadeOut();
     $(this).removeClass('close');
 });

。通过

$('.main h2').mouseover(function () {
   $(this).siblings('.answer').show();
   $(this).siblings('.answer').slideDown();
   $(this).addClass('close');
});
$('.main h2').mouseout(function () {
   $(this).siblings('.answer').hide();
   $(this).siblings('.answer').fadeOut();
   $(this).removeClass('close');
});

它会帮助你。

答案 1 :(得分:1)

试试这种方式;]

&#13;
&#13;
$(document).ready(function () {
    $('.answer').hide();

    $('.main h2').on('mouseover', function () {
        $(this).next().slideDown(300);
        $(this).addClass('close');
    });
    $('.main h2').on('mouseout', function () {
        $(this).next().slideUp(300);
        $(this).removeClass('close');
    });           

}); // end ready
&#13;
h2 {
    background: url(_images/open.png) no-repeat 0 11px;
    padding: 10px 0 0 25px;
    cursor: pointer;
}

    h2.close {
        background-image: url(_images/close.png);
    }

.answer {
    margin-left: 25px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
        <header>

        </header>
        <div class="content">
            <div class="main">
                <h1>A One Page FAQ</h1>
                <div class="faq">
                    <h2>I've heard that JavaScript is the long-lost fountain of youth. Is this true?</h2>
                    <div class="answer">
                        <p>Why, yes it is! Studies prove that learning JavaScript freshens the mind and extends life span by several hundred years. (Note: some scientists disagree with these claims.)</p>
                    </div>
                </div>
                <div class="faq">
                    <h2>Can JavaScript really solve all of my problems?</h2>
                    <div class="answer">
                        <p>Why, yes it can! It's the most versatile programming language ever created and is trained to provide financial management advice, life-saving CPR, and even to take care of household pets.</p>
                    </div>
                </div>
                <div class="faq">
                    <h2>Is there nothing JavaScript <em>can&#8217;t</em> do?</h2>
                    <div class="answer">
                        <p>Why, no there isn&#8217;t! It&#8217;s even able to write its own public relations-oriented Frequently Asked Questions pages. Now that&#8217;s one smart programming language!</p>
                    </div>
                </div>
            </div>
        </div>
        <footer>
            <p>JavaScript &amp; jQuery: The Missing Manual, 3rd Edition, by <a href="http://sawmac.com/">David McFarland</a>. Published by <a href="http://oreilly.com/">O'Reilly Media, Inc</a>.</p>
        </footer>
    </div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这是因为您使用类选择器实际上选择了所有具有此类$('.answer').show();

的元素

您需要更改代码以选择最接近问题的答案并将其显示为

            $('.main h2').mouseover(function (e) {
               $('.answer').hide();//Hide All Other Answers
               $(e).closest('.answer').show();//Show Closest Answer To Question


            });

和鼠标输出

$('.main h2').mouseout(function (e) {
   $('.answer').hide();
});

答案 3 :(得分:0)

使用$ .next()。刚才你要更改所有类。要显示所有类。并且所有.answer to slideDown(在$(document).ready()中你从整个文档中获取元素($ this)以及稍后运行的代码它)

试试这个

$('.main h2').mouseover(function () {
    $(this).next('.answer').show();
    $(this).next('.answer').slideDown();
    $(this).addClass('close');
});

你也应该删除这两行代码。

$('.answer').hide();
var $answer = $(this).next('.answer');

并应用display:hidden;到你的.answer CSS课程。

答案 4 :(得分:0)

试试这个

$(document).ready(function () {
    $('.answer').hide();
    var $answer = $(this).next('.answer');
    $('.main h2').mouseover(function () {
        $(this).find('.answer').show();
        $answer.slideDown();
        $(this).addClass('close');
    });
    $('.main h2').mouseout(function () {
        $(this).find('.answer').hide();
        $answer.fadeOut();
        $(this).removeClass('close');
    });

}); //