jquery - 为什么不能将鼠标悬停在第一项上

标签 jquery html hover

我尝试在位于 div 中的未排序列表上使用 jQuery hover() 方法。 当将鼠标移到其中一个列表项上时没有任何反应,但是当将其移到第二个列表项上时, react 是程序化的(下划线)。如果我然后回到第一个项目,它也会按照程序使用react,那么为什么它第一次不这样做呢?如果我使用 div 而不是 li,或者当我将 preventDefault() 添加到 jQuery 方法时,也会出现同样的问题。我看过问题的解决方案 hover not working for items in list但它们也不起作用。

这是 html 代码:

<div id="div1">
    <ul>
        <li id="res0">first line</li>
        <li id="res1">second line</li>
        <li id="res2">third line</li>
    </ul>
</div>

和脚本:

$("#div1").hover(function() {

    $("#res0").hover(function() {
        $(this).css("text-decoration","underline");
        document.body.style.cursor="pointer";
    }, function(){
        $(this).css("text-decoration","none");
        document.body.style.cursor="default";
    });

    $("#res1").hover(function() {
        $(this).css("text-decoration","underline");
        document.body.style.cursor="pointer";
    }, function(){
        $(this).css("text-decoration","none");
        document.body.style.cursor="default";
    });

    $("#res2").hover(function() {
        $(this).css("text-decoration","underline");
        document.body.style.cursor="pointer";
    }, function(){
        $(this).css("text-decoration","none");
        document.body.style.cursor="default";
    });
}); 

如果有人能告诉我哪里出了问题,我将不胜感激!

这是网址:http://www.arcro.nl/tests/test3.php

最佳答案

你可以像这样减少你的代码,

$("#div1 li").hover(function () {
    $(this).css("text-decoration", "underline");
    document.body.style.cursor = "pointer";
}, function () {
    $(this).css("text-decoration", "none");
    document.body.style.cursor = "default";
});

Fiddle

关于jquery - 为什么不能将鼠标悬停在第一项上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33956065/

相关文章:

javascript - 如何在 DIV 中给第二个标签加下划线

jquery - 随着鼠标移动移动信息框或工具提示

javascript - 光标悬停时播放的音乐

javascript - 在循环内分配 json 值时值被覆盖

javascript - 单击几下后如何禁用按钮功能?

javascript - 本地 HTML 文件与(读/写)本地 .xlsm Excel 文件通信的最佳方式

javascript - 使用 ngSanatize 的 HTML5 音频和 Angular 双向绑定(bind)问题

css - 在纯 css 中禁用悬停效果

javascript - 多级列表中的大写首字母(JS、JQUERY)

javascript - 当窗口等于 div 时显示 div,但当窗口超过 div 时隐藏