简体   繁体   English

QTip2多个元素,相同的工具提示

[英]QTip2 Multiple Elements, Same Tooltip

I have a page in which I dynamically create elements that need tooltips. 我有一个页面,我动态创建需要工具提示的元素。

I've tried a few different approaches and looked online for some answers to no avail. 我尝试了一些不同的方法,并在网上寻找一些无济于事的答案。

As of now, I have this: 截至目前,我有这个:

var $links = $('a.link');
var len = $links.length;
for(var i = 0; i < len; i++){
    var $ele = $($links[i]);
    $ele.qtip({
        id: 'editLink',
        overwrite: false,
        content: {
            text: $linkEditor,
            title: {
                text: 'Edit Link',
                button: true
            }
        },
        position: {
            my: 'top center',
            at: 'bottom center',
            viewport: $(window)
        },
        show: {
            event: 'mouseover',
            solo: true
        },
        hide: false,
        style: {classes: 'ui-tooltip-shadow ui-tooltip-red'}
    });
}

What I'm looking for is some way to have all of these elements use the exact same tooltip. 我正在寻找的是让所有这些元素使用完全相同的工具提示的某种方式。 I want them all to use the exact same content (in this case a single form) and reference the tooltip in the exact same way (by the tooltip element with id 'ui-tooltip-editLink'). 我希望他们都使用完全相同的内容(在这种情况下是单个表单)并以完全相同的方式引用工具提示(通过id为'ui-tooltip-editLink'的工具提示元素)。

With what I have, it currently creates the first tooltip correctly, but if I add a new element and reassign tooltips, it creates a whole new tooltip with a different id for the new element. 凭借我所拥有的,它当前正确地创建了第一个工具提示,但是如果我添加一个新元素并重新分配工具提示,它会为新元素创建一个具有不同id的全新工具提示。

Does anyone know some way of accomplishing a multiple elements, same tooltip approach? 有谁知道实现多元素的一些方法,相同的工具提示方法?

I've figured out how to have one tooltip div be shared by many tooltip images if anyone finds it handy 我已经想出如果有人发现方便的话,如何让许多工具提示图像共享一个工具提示div

 $(".tooltipBearing").qtip({
                            content: {  
                                text: $("#tooltipDiv").html()
                            }          
                      });

If you fail to put the .html() on there, you will see the shared tooltip show up once, and then when you activate it from another image, it will no longer work for the first one... 如果你没有把.html()放在那里,你会看到共享工具提示出现一次,然后当你从另一个图像激活它时,它将不再适用于第一个...

The tooltipBearing is a class set on some images in the page. tooltipBearing是页面中某些图像的类集。

tooltipDiv is the ID of the div containing your tooltip content. tooltipDiv是包含工具提示内容的div的ID。

In order to use qtip in dynamically created element, you need to include qtip during the element initialization. 为了在动态创建的元素中使用qtip ,您需要在元素初始化期间包含qtip

eg 例如

$('<div style="top:0px; left:0px; position:absolute;">abc123</div>').appendTo("body").qtip({content: "test"});

Sadly it looks like we need to clone the Tooltip element for each of the hover/click elements. 遗憾的是,我们需要克隆每个悬停/点击元素的Tooltip元素。

Craig mentions this in the GitHub issue: https://github.com/Craga89/qTip2/issues/173 Craig在GitHub问题中提到了这一点: https//github.com/Craga89/qTip2/issues/173

This is what I did. 这就是我做的。 I attached the my tooltip on a unique element. 我将我的工具提示附加在一个独特的元素上。 And then all of my other elements that need the same tooltip will just trigger the unique element's tooltip display: 然后我需要相同工具提示的所有其他元素将只触发独特元素的工具提示显示:

$('#unique-element').qtip({
  content: {text: 'My Tooltip'},
});

$('.other-elements').click(function(){
  $('#unique-element').qtip('show');
});

However, the all of your tooltips will be displayed in a fixed position. 但是,所有工具提示都将显示在固定位置。 This was not a problem in my case because I was using the tooltip as modal. 这在我的案例中不是问题,因为我使用工具提示作为模态。 You will have to manage the tooltip position via cutom event methods. 您必须通过cutom event方法管理工具提示位置。

Based on the answer of user738048 , my working solution is like this: 基于user738048的答案 ,我的工作解决方案是这样的:

$('[data-tooltipid]').each(function() {
    var ttid = $(this).data('tooltipid');
    $(this).qtip({
        content: {
            text: $('#'+ttid).html(),
        },
        position: {
            my: 'top left',
            at: 'bottom left',
            target: $(this)
        }
    }).removeAttr('href').removeAttr('onclick');
});

In my case, the id of the HTML element containing the text is taken from a data-tooltipid attribute; 在我的例子中,包含文本的HTML元素的id取自data-tooltipid属性; my href and onclick attributes are removed since they are obsoleted by the tooltip. 我的hrefonclick属性被删除,因为它们被工具提示废弃了。

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

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