简体   繁体   English

工具提示在表格单元格中不起作用

[英]tooltip doesn't work in a table cell

My question might seems long, but I am trying to give relevant information in detail so please bear with me. 我的问题似乎很长,但我想详细提供相关信息,所以请耐心等待。

I am using tooltipster tooltip plugin ( http://iamceege.github.io/tooltipster/ ) on my website. 我在我的网站上使用tooltipster工具提示插件( http://iamceege.github.io/tooltipster/ )。 However my question is more related to JS instead of tooltipster plug in. I am easily able to integrate the tooltip plugin on my website and it is working just fine on images, buttons, etc where the content is static. 但是我的问题与JS更相关,而不是工具提示插件。我可以轻松地将工具提示插件集成到我的网站上,它可以很好地处理内容静态的图像,按钮等。 However, I am populating tables dynamically based on the JSON response and I can't get the tooltip working on the table cells. 但是,我根据JSON响应动态填充表,我无法获得工作提示在表格单元格上工作。

First I thought there might be some problem with the plugin. 首先我认为插件可能存在一些问题。 In a flash I switched to bootstrap inbuilt tooltip and I am facing the same problem. 在一瞬间,我切换到bootstrap内置的工具提示,我面临同样的问题。 I can get bootstrap tooltip to work on other other elements apart from the table cells. 我可以使用bootstrap工具提示来处理除表格单元格之外的其他元素。

Here is my JS where I am adding the class customToolTip 这是我的JS,我在这里添加了类customToolTip

function addBetTypeProductsToLegend(table) {
    var betType = $.data(table, 'BetTableData').betType,
        legendRow = $.data(table, 'BetTableData').legendRow,
        productNotFount = true,
        td;
    $.each(betType.products, function(index,value) {
        var betTypeHint = 'betTypeId_' + value.id + '_hint';
        td = element.create('td');
        element.setId(td, value.id);
        element.setClassName(td, 'customToolTip'); // adding customToolTip class
        element.setTitle(td, window[ 'betTypeId_' + value.id + '_hint']); // setting respective title tags
        element.setInnerHtml(td, getBrandBetName(value.name));
        legendRow.appendChild(td);
        productNotFount = false;
    });

    // Racing Specials
    td = element.create('td');
    element.setId(td, betType.id);
    element.setInnerHtml(td, betType.name);

    if (productNotFount) legendRow.appendChild(td);
}

This is how I am assigning class name and title attributes 这就是我分配类名和标题属性的方法

var element = {
    create: function(htmlIndentifier) {
        return document.createElement(htmlIndentifier);
    },
    setClassName: function(element, className) {
        element.className = className;
    },
    setTitle: function(element, title) {
    element.title = title;
    }
}

JS plugin initialization JS插件初始化

$(document).ready(function() {
    $('.customToolTip').tooltipster({
        animation: 'fade',
        delay: 400,
        theme: 'tooltipster-IB',
        touchDevices: false,
        trigger: 'hover'
    });
});

So the above doesn't apply to the table cell I tried to use bootstrap tooltip and initialize the function like this. 所以上面不适用于表格单元格我尝试使用bootstrap工具提示并初始化这样的函数。

$(".customToolTip").tooltip({
    placement : 'top'
});

It works fine for other elements apart from table cells. 它适用于除表格单元格之外的其他元素。

I tried to set padding as well for tooltip as well 我也试图为工具提示设置填充

$('.customToolTip').each(function(e) {
    var p = $(this).parent();
    if(p.is('td')) {
        $(this).css('padding', p.css('padding'));
        p.css('padding', '0 0');
    }
    $(this).tooltip({
        placement: 'top'
    });
});

I followed couple of other posts where people were facing problem with tooltip and table cells 我跟着几个其他帖子,其中人们面临着工具提示和表格单元格的问题
bootstrap "tooltip" and "popover" add extra size in table bootstrap“tooltip”和“popover”在表格中添加额外的大小
https://github.com/twbs/bootstrap/issues/5687 https://github.com/twbs/bootstrap/issues/5687

Seems like I have deal with the layout issue later on first I need to show the custom tooltip instead of default title attribute. 好像我稍后处理布局问题首先我需要显示自定义工具提示而不是默认标题属性。

Your initialization starts on document ready, when table does not exist in DOM yet. 当DOM中尚不存在表时,您的初始化将从文档就绪开始。 So $('.customToolTip') does not find those. 所以$('。customToolTip')找不到那些。

What you need is to make function, something like this: 你需要的是制作功能,如下所示:

function BindTooltips(target) {
    target = target || $(document);

    target.find('.customToolTip').tooltipster({
        animation: 'fade',
        delay: 400,
        theme: 'tooltipster-IB',
        touchDevices: false,
        trigger: 'hover'
    });
}

And launch it on document ready, so it will find all .customToolTip in your document. 并在文档就绪时启动它,因此它将在您的文档中找到所有.customToolTip。

Then after your table is being inserted into dom launch this function again and provide container (table) as argument. 然后在将表插入dom后再次启动此函数并提供容器(表)作为参数。

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

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