简体   繁体   English

jQuery无法使用hover()方法识别数据表的“ tbody”和“ tr”

[英]JQuery doesn't recognize 'tbody' and 'tr' of datatable using hover() method

So I am working with a datatable and trying to create a hover window which will display data from the datatable and a picture, not important, thing is I am trying to append a div with premade css classes for this project into either a cell or row using hover(), but jquery doesn't recognize the body and row of the datatable. 因此,我正在使用数据表并尝试创建一个悬停窗口,该窗口将显示数据表中的数据和图片,这并不重要,我正在尝试将带有该项目的预制css类的div附加到单元格或行中使用hover(),但jquery无法识别数据表的主体和行。 Here is a fiddle I tested it on https://jsfiddle.net/r6tbv9uz/6/ and it works just fine, and here is a code I am using in the project: 这是我在https://jsfiddle.net/r6tbv9uz/6/上测试过的小提琴,它工作得很好,这是我在项目中使用的代码:

$('.datatable-sort').find('tbody').hover(
    function () {
        console.log('hovered in');
        var iDiv = CreateHoverElement();//the div is a bit complex so I have separate function for it
        var element = $(this).closest('tr');
        element.append(iDiv);
    },
    function () {
        var element = document.getElementById('tablehover');
        element.parentNode.removeChild(element);
        console.log('hovered out');
    }
);

The thing is when I use the hover as such: 问题是当我像这样使用悬停时:

$('.datatable-sort').hover(function(){...},function(){...})

The hover function works perfectly but that wont help me as I need the hover out part to work within a row and only in tbody. 悬停功能可以完美地工作,但是这对我没有帮助,因为我需要悬停零件才能在一行中唯一地工作。 I have tried lot of googling around and lot of trial and error but can't figure it out, if anyone has ideas i would appriciate it, Thanks. 我已经尝试了很多谷歌搜索和反复试验,但是却无法解决,如果有人有想法可以应用,谢谢。

EDIT: html snippet of the datatable: 编辑:数据表的html代码段:

<table class="table table-striped datatable-sort compact fixedHeader-locked">
                <thead>
                    <tr>
                        <th scope="col">Time</th>               <!--Created-->
                        <th scope="col">Lane</th>               <!--LaneNumber-->
                        <th scope="col">Credence(%)</th>        <!--Credence-->
                        <th scope="col">LPN</th>                <!--Plate-->
                        <th scope="col">LPN(%)</th>             <!--PlateConfidence-->
                        <th scope="col">Country</th>            <!--CountryCode-->
                        <th scope="col">Country(%)</th>         <!--CountryConfidence-->
                        <th scope="col">Speed(km/h)</th>        <!--Speed-->
                        <th scope="col">Speed change(km/h)</th> <!--SpeedDifference-->
                        <th scope="col">Width(cm)</th>          <!--Width-->
                        <th scope="col">Height(cm)</th>         <!--Height-->
                        <th scope="col">Length(cm)</th>         <!--Length-->
                        <th scope="col">Weight(kg)</th>         <!--Weight-->
                        <th scope="col">Axles</th>              <!--Axles-->
                        <th scope="col">VehicleID</th>          <!--ID-->
                        <th scope="col">ClassEUR13</th>         <!--Classification-->
                        <th scope="col">Version</th>            <!--null-->
                        <th scope="col">Title</th>              <!--null-->
                        <th scope="col">Direction</th>          <!--Direction-->
                        <th scope="col">Certainty</th>          <!--null-->
                        <th scope="col">Axles Count</th>        <!--AxleCount-->
                        <th scope="col">Gross Weight</th>       <!--null-->
                        <th scope="col">Axles 1 weight(kg)</th> <!--Axles[0].Weight-->
                        <th scope="col">Axles 2 weight(kg)</th> <!--Axles[1].Weight-->
                        <th scope="col">Heading (sec)</th>      <!--Height-->
                        <th scope="col">Gap (sec)</th>          <!--Gap-->
                        <th scope="col">Digital Signature</th>  <!--null-->
                        <th scope="col">Checksum</th>           <!--Checksum-->
                    </tr>
                </thead>
            </table>

Rows are added to the table via JS script. 通过JS脚本将行添加到表中。

It sounds like you are adding these elements dynamically. 听起来您正在动态添加这些元素。 If that is correct, then the event handlers when they are defined are not able to attach to the elements themselves because they are not present in the DOM at the time of binding. 如果这是正确的,那么定义事件处理程序时将无法将其附加到元素本身,因为在绑定时它们不存在于DOM中。 To solve this, try using dynamic event handlers: 要解决此问题,请尝试使用动态事件处理程序:

$('.datatable-sort').on('mouseenter', 'tbody', function () {
        console.log('hovered in');
        var iDiv = CreateHoverElement();//the div is a bit complex so i have separate function for it
        var element = $(this).closest('tr');
        element.append(iDiv);
    }).on('mouseleave', 'tbody', function () {
        var element = document.getElementById('tablehover');
        element.parentNode.removeChild(element);
        console.log('hovered out');
    });

I'm not aware of a way to delegate the .hover() event but according to the docs for .hover() , it is a shorthand syntax for two events: mouseenter and mouseleave , so delegating each of these should provide the same effect. 我不知道委托.hover()事件的方法,但是根据.hover()的文档,它是两个事件的.hover()语法: mouseentermouseleave ,因此委派每个事件都应提供相同的效果。

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

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