简体   繁体   English

尝试使用JQuery突出显示表行但不起作用,有人可以告诉我为什么以及如何解决它吗?

[英]Trying to highlight table row using JQuery but not working, can anyone tell me why and how to fix it?

Trying to hightlight table row using JQuery but not working, can anyone tell me why and how to fix it? 尝试使用JQuery突出显示表行但不起作用,有人可以告诉我原因以及如何解决它吗?

table: 表:

<table id="customers">
    <caption>Customers</caption>
    <thead>
        <tr>
            <th>Customer no</th>
            <th>Last name</th>
            <th>First name</th>
            <th>Options</th>
        </tr>
    </thead>
    <tbody>
        <tr class="alt">
            <td>1</td>
            <td>Jackson</td>
            <td>Simon</td>
            <td>
                <div class="align-left">
                    <input type="submit" name="View" id="view" value="View" />
                </div>
                <div class="align-right">
                    <input type="submit" name="Rent" id="rent" value="Wants to rent" />
                </div>
            </td>
        </tr>
        <tr class="alt">
            <td>1</td>
            <td>Miller</td>
            <td>Darren</td>
            <td>
                <div class="align-left">
                    <input type="submit" name="View" id="view" value="View" />
                </div>
                <div class="align-right">
                    <input type="submit" name="Rent" id="rent" value="Wants to rent" />
                </div>
            </td>
        </tr>
    </tbody>
</table>

JQuery: JQuery的:

    $("#customers tbody tr").hover(

    function () {
        $(this).css({
            background: 'yellow'
        });
    },

    function () {
        $(this).css("background", "");
    });

What happens is the table row does not get highlighted when hovering over it, please also note that it works for a td when i adjust the selector, just wont work for tr row. 发生的情况是,将鼠标悬停在表格行上时,表格行未突出显示,请注意,当我调整选择器时,该表格行适用于td,而不适用于tr行。

css: CSS:

    ...
    /* table data style */

    table {

        border-collapse: collapse;    
        width: 400px;
        color: grey;
        font-family: sans-serif;

    }

    th, td {

        border-bottom: 1px solid brown;
        padding: 5px;
        background-color: seashell;
        text-align: left;    

    }

    th {

        width: 200px;

    }

    td {

        width: 200px;

    }

    tr.alt td, tr.alt th {

        background-color: white;

    }

    tr:last-child td, tr:last-child th {

        border-bottom: 0;

    }

    caption {

        font-weight: bold;
        padding-bottom: 5px;

    }

    .main-font {

        font-family: sans-serif;

    }

I'll add to sbeliv01's correct answer that there is no need for Javascript, it is realizable with pure CSS, and I would strongly advise you to use it for this sort of thing: 我将添加到sbeliv01的正确答案中,即不需要Javascript,它可以通过纯CSS实现,并且我强烈建议您将其用于此类事情:

#customers tbody tr:hover
{
  background: yellow;
}

EDIT after OP post full CSS code : Yes, your CSS is "blocking" the hover effect, because you are applying background-color on your td elements. 在OP之后编辑完整的CSS代码后进行编辑 :是的,您的CSS正在“阻止”悬停效果,因为您正在将td元素应用background-color So your tr background color is correctly changed yellow , but you can't see it as your td s are still seashell / white . 因此,您的tr背景颜色正确更改为yellow ,但由于td仍为seashell / white因此看不到它。

To fix your problem, apply background color to tr s in your CSS and not to your td s: 要解决您的问题,请将背景颜色应用于CSS中的tr而不是td

table {

    border-collapse: collapse;
    width: 400px;
    color: grey;
    font-family: sans-serif;

}

th, td {
    /* Remove background-color here */
    border-bottom: 1px solid brown;
    padding: 5px;
    text-align: left;

}

/* Add this declaration (with the color previously applied to "th, td") */
tr
{
    background-color: seashell;
}

th {

    width: 200px;

}

td {

    width: 200px;

}

/* Change your selector from "tr.alt td, tr.alt th" to this */
tr.alt {

    background-color: white;

}

tr:last-child td, tr:last-child th {

    border-bottom: 0;

}

caption {

    font-weight: bold;
    padding-bottom: 5px;

}

.main-font {

    font-family: sans-serif;

}

If that is the exact code that you're using, you have an extra }); 如果您使用的是正确的代码,则可以使用}); at the end of the jQuery that would throw an error. 在jQuery的末尾会引发错误。 Swap it out with the code below and it should work fine. 用下面的代码替换掉它,它应该可以正常工作。

$("#customers tbody tr").hover(
    function ()
    {
        $(this).css({background: 'yellow'});
    },

    function ()
    {
        $(this).css("background", "");
    }
);

You can try this one 你可以试试这个

$('#customers tbody tr').mouseenter(function(){
    $(this).css({ background: 'yellow' });
}).mouseleave(function(){
    $(this).css({ background: '' });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM