jquery - 将鼠标悬停在链接上时显示相邻的隐藏 div

标签 jquery html

我有以下 HTML 和 Javascript。我只想仅当鼠标悬停在链接上时一次显示隐藏的一个。如果鼠标位于链接“Drink”上,则仅显示该链接下方的隐藏 div,其他隐藏 div 必须保持隐藏。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>
            Untitled Document
        </title>
        <script type="text/javascript" src="jquery.js">
        </script>
        <script type="text/javascript">
            $(document).ready(function() {


// timer for hiding the div
var hideTimer;

// show the DIV on mouse over
$(".show_div").mouseover(function() {
    // forget any hiding events in timer
    clearTimeout( hideTimer );
    $(".hello").css('visibility', 'visible');
});

$(".hello").mouseover(function() {
    clearTimeout( hideTimer );
    $(".hello").css('visibility', 'visible');
});

// set a timer to hide the DIV
$(".show_div").mouseout(function() {
    hideTimer = setTimeout( hideHello, 333 );
});

$(".hello").mouseout(function() {
    hideTimer = setTimeout( hideHello, 333 );
});

// hides the DIV
function hideHello() {
    $(".hello").css('visibility', 'hidden');
}

                });
        </script>

    </head>

    <body>
<br/>



<table border="1" width="400">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td><a class="show_div" href="#">Drink</a>
    <div class="hello" style="visibility:hidden;z-index:999;position: absolute;background-color:#99CC67;">
        <ul>
            <li>
                Coffee
            </li>
            <li>
                Tea
            </li>
            <li>
                Milk
            </li>
        </ul>
    </div>
</td>
<td><a class="show_div" href="#">Friuts</a>
    <div class="hello" style="visibility:hidden;z-index:999;position: absolute;background-color:#99CC67;">
        <ul>
            <li>
                Banana
            </li>
            <li>
                Water Melon
            </li>
            <li>
                Lychee
            </li>
        </ul>
    </div>
</td>
</tr>
<tr>
<td><a class="show_div" href="#">Movies</a>
    <div class="hello" style="visibility:hidden;z-index:999;position: absolute;background-color:#99CC67;">
        <ul>
            <li>
                Avatar
            </li>
            <li>
                Star War
            </li>
            <li>
                Titanic
            </li>
        </ul>
    </div>
</td>
<td><a class="show_div" href="#">Books</a>
    <div class="hello" style="visibility:hidden;z-index:999;position: absolute;background-color:#99CC67;">
        <ul>
            <li>
                Novel
            </li>
            <li>
                History
            </li>
            <li>
                Design
            </li>
        </ul>
    </div>
</td>
</tr>
</table> 

<br/>
    </body>

</html>

谁能帮我解决这个问题吗?谢谢。

最佳答案

除了你想要实现的目标之外,还有其他选择。您可以使用其他方法来获得相同的结果,而不是坚持使用此方法。

请参阅下面的示例,该示例最初来自 scottonwriting ,但我让样本看起来像你的。这是按照 scottonwriting 上的建议使用工具提示的示例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>
            Untitled Document
        </title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
        </script>
        <style type="text/css">
            .skmTooltipHost { border-bottom: dotted 1px brown; } .skmTooltipContainer
            { padding-left: 20px; padding-right: 10px; padding-top: 3px; padding-bottom:
            3px; display: none; position: absolute; background-color: #ff9; border:
            solid 1px #333; width :300px; z-index: 999; }
        </style>
        <script type="text/javascript">
            $(document).ready(function() {


                $(".skmTooltipHost").hover(

                function() {
                    $(this).append('<div class="skmTooltipContainer">' + $(this).attr('tooltip') + '</div>');

                    $(this).find('.skmTooltipContainer').css("left", $(this).position().left + 20);
                    $(this).find('.skmTooltipContainer').css("top", $(this).position().top + $(this).height());
                    $(".skmTooltipContainer").fadeIn(10);
                },

                function() {
                    $(".skmTooltipContainer").fadeTo(10, 0.1, function() {
                        $(this).remove();
                    });
                });

            });
        </script>
    </head>

    <body>
        <br/>
        <table border="1" width="400">
            <tr>
                <th>
                    Header 1
                </th>
                <th>
                    Header 2
                </th>
            </tr>
            <tr>
                <td>
                    <p>
                        <span tooltip="Choose your drink:&lt;div&gt;&lt;ul&gt;&lt;li&gt;Coffe&lt;/li&gt;&lt;li&gt;Tea&lt;/li&gt;&lt;li&gt;&lt;a target=_blank href=http://www.w3schools.com&gt;Visit W3Schools.com!&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;"
                        class="skmTooltipHost">
                            Drinks
                        </span>
                    </p>
                </td>
                <td>
                    <p>
                        <span tooltip="Choose your fruit:&lt;div&gt;&lt;ul&gt;&lt;li&gt;Orange&lt;/li&gt;&lt;li&gt;Banana&lt;/li&gt;&lt;li&gt;&lt;a target=_blank href=http://www.w3schools.com&gt;Visit W3Schools.com!&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;"
                        class="skmTooltipHost">
                            Fruit
                        </span>
                    </p>
                    </a>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <span tooltip="Choose your movie:&lt;div&gt;&lt;ul&gt;&lt;li&gt;Avatar&lt;/li&gt;&lt;li&gt;Titanic&lt;/li&gt;&lt;li&gt;&lt;a target=_blank href=http://www.w3schools.com&gt;Visit W3Schools.com!&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;"
                        class="skmTooltipHost">
                            Movie
                        </span>
                    </p>
                    </a>
                </td>
                <td>
                    <p>
                        <span tooltip="Choose your book:&lt;div&gt;&lt;ul&gt;&lt;li&gt;History&lt;/li&gt;&lt;li&gt;Novel&lt;/li&gt;&lt;li&gt;&lt;a target=_blank href=http://www.w3schools.com&gt;Visit W3Schools.com!&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;"
                        class="skmTooltipHost">
                            Drink
                        </span>
                    </p>
                </td>
            </tr>
        </table>
        <br/>
        <br/>
    </body>

</html>

总有一个替代方案。希望这有帮助。

关于jquery - 将鼠标悬停在链接上时显示相邻的隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3124719/

相关文章:

jquery - 关于实现的完整日历问题

javascript - 我想在提交表单后打开模式?

javascript - jQuery 字体选择器 : show&hide with focus

php - 使用 Blade 显示 HTML 显示 HTML 代码

javascript - keyup 不适用于动态加载的文本区域

javascript - 函数未识别 javascript/ajax SOAP 客户端

javascript - 使用 jQuery 进行搜索后,搜索栏就会消失

html - 如何使 div 中的图标始终居中?

html - CSS 子选择器 - 需要定位子元素

html - 没有 PHP 的简单 HTML 上传表单