将鼠标悬停在表上,用jquery更改另一个元素样式,只适用于1毫秒

hover on table to change another element style with jquery, works for only 1 mil second

本文关键字:另一个 元素 样式 1毫秒 适用于 jquery 悬停 鼠标      更新时间:2023-09-26

当悬停在某个表的任何区域时,我希望文本加下划线。

这是我的代码,它只工作1密耳秒,然后下划线消失,即使光标在表内(尝试了gofoo测试):

JS:

<script>
$(document).ready(function(){
$(function(){
    $('.foo').mouseenter(function(){
        $(".vvv").addClass('altbg')
    }).mouseout(function(){
        $(".vvv").removeClass('altbg')
    });
});
})
</script>

CSS:

.altbg {
    text-decoration:underline;
}

HTML:

<div class="go">
<table border="1" class="foo">
        <col style="width:115px;" />
        <col style="width:280px;" />
        <col style="width:125px;" />
        <col style="width:145px;" />
        <col style="width:125px;" />
        <col style="width:230px;" />
          <tr>
            <td>0</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
          </tr></table>
</div>
<div class="vvv">Hello</div>  

编辑:第二个相关问题:

我希望在悬停表格时更改文本颜色,即使颜色已经由ID定义。

除了将ID更改为class之外,我如何让这个div更改它的颜色?

也许为了将类指定给选择器,脚本中的某些内容需要更改,但我不知道该怎么做

JS:

<script>
$(document).ready(function(){
$(function(){
    $('td').mouseenter(function(){
        $(".vvv").addClass('altbg')
    }).mouseout(function(){
        $(".vvv").removeClass('altbg')
    });
});
})
</script>

CSS:

#meh {color:red}
.altbg {
    text-decoration:underline;
    color:blue;
}

HTML:

<div class="go">
<table border="1" class="foo">
        <col style="width:115px;" />
        <col style="width:280px;" />
        <col style="width:125px;" />
        <col style="width:145px;" />
        <col style="width:125px;" />
        <col style="width:230px;" />
          <tr>
            <td>0</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
          </tr></table>
</div>
<div id="meh" class="vvv">Hello</div>    

不是悬停的时间,而是鼠标的位置使其不会出现更长时间。由于某些原因,针对table的类将不起作用,相反,您必须针对其中的td元素:

$(document).ready(function () {
    $(function () {
        $('td').mouseenter(function () {
            $(".vvv").addClass('altbg')
        }).mouseout(function () {
            $(".vvv").removeClass('altbg')
        });
    });
})
.altbg {
    text-decoration:underline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="go">
    <table border="1" class="foo">
        <col style="width:115px;" />
        <col style="width:280px;" />
        <col style="width:125px;" />
        <col style="width:145px;" />
        <col style="width:125px;" />
        <col style="width:230px;" />
        <tr>
            <td>0</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </table>
</div>
<div class="vvv">Hello</div>

以下是jsfiddle演示:http://jsfiddle.net/0uqboeat/也

它有效,唯一的问题是您需要(td)而不是(foo)。所以(td)是表格单元格。

演示

http://jsfiddle.net/u0dz0xtu/

$(document).ready(function(){
$(function(){
    $('td').mouseenter(function(){
        $(".vvv").addClass('altbg')
    }).mouseout(function(){
        $(".vvv").removeClass('altbg')
    });
});
})