简体   繁体   English

jQuery关于动态元素的提示

[英]jquery cluetip on dynamic elements

I can't seem to get this to work properly. 我似乎无法正常工作。

I am writing several links to the DOM (looping through a json file and appending to the DOM), and then I need those elements to trigger tooltips on hover. 我正在编写指向DOM的几个链接(通过json文件循环并附加到DOM),然后我需要那些元素来触发悬停时的工具提示。

I'm not seeing a good example of this method anywhere - the cluetip site shows a brief example, of looking for the a and calling cluetip then. 我在任何地方都没有看到这种方法的一个很好的例子-cluetip网站显示了一个简短的示例,该示例寻找a然后调用cluetip。 I'm thinking there must be a .live or .delegate way to do this: 我在想必须使用.live或.delegate方式来做到这一点:

$("body").delegate("a.toolTip", "mouseover", function (event) {

            $('a.toolTip').cluetip({
                showTitle: false,
                attribute: 'title',
                local: false
                });

            event.preventDefault();

        });

but this doesn't trigger the first mouseover and I get a "sorry, contents could not be loaded" 但这不会触发第一次鼠标悬停,并且我得到一个“抱歉,无法加载内容”

Any ideas? 有任何想法吗?

thanks 谢谢

You need to re-trigger the mouseover event. 您需要重新触发mouseover事件。

$("body").delegate("a.toolTip", "mouseenter", function (event) {
    $('a.toolTip').cluetip({
        showTitle: false,
        attribute: 'title',
        local: false
    }).trigger("mouseenter");

    event.preventDefault();
});

Additional nitpick things: 其他挑剔的事情:

event.preventDefault() should come first , and you should prevent the plugin from being applied multiple times. event.preventDefault()应该排在最前面 ,并且您应该防止插件被多次应用。

$("body").delegate("a.toolTip:not(.hasTooltip)", "mouseenter", function (event) {

    $('a.toolTip').cluetip({
        showTitle: false,
        attribute: 'title',
        local: false
    }).addClass("hasTooltip").trigger("mouseenter");
    event.preventDefault();
});

Edit: mouseover should have been mouseenter, and the event.preventDefault really should be last so that if it does fail, the default tooltip will still work. 编辑:mouseover应该已经是mouseenter了,event.preventDefault实际上应该是last,因此,如果它确实失败了,则默认的工具提示仍将起作用。

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

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