简体   繁体   English

JQuery 工具提示:标题属性内的目标跨度

[英]JQuery tooltip: Target Span inside title attribute

I have a div with the following code :我有一个带有以下代码的 div:

<div id='mydiv' title="<span id='myspan'>Hi there</span>">

I'm using the JQuery's tooltip我正在使用 JQuery 的工具提示

            $("#mydiv").tooltip({
                content: function () {
                    return $(this).prop('title');
                },
               open: function (event, ui) {
                   ui.tooltip.css("max-width", "800px")

               },
               position:{
                    my:"left+10 top+25",
                    at:"left top"
               }
            });

The Tooltip works fine, however I'm unable to target the span inside the tooltip, $('#myspan').html() returns null.工具提示工作正常,但我无法定位工具提示内的跨度, $('#myspan').html()返回 null。

Any ideas ?有任何想法吗 ?

Thanks !谢谢 !

It is because of how the jQuery tooltip works.这是因为 jQuery 工具提示的工作方式。 Every time you hover your div a element is created with the tooltip and attached to DOM.每次将 div 悬停时,都会使用工具提示创建一个元素并附加到 DOM。 Every time you remove hover this element is removed from the DOM.每次你移除悬停时,这个元素都会从 DOM 中移除。 So you can not target it.所以你不能瞄准它。 There isn't at the DOM. DOM 上没有。 In the DOM its only when you hover your div.在 DOM 中,只有当您将 div 悬停时。

Here an example to understand this better:这是一个更好地理解这一点的例子:

$( "#mydiv" ).hover(
  function() {
    alert($('#myspan').html());
  });

This will give you the tooltip element.这将为您提供工具提示元素。 But unless you hover in your div cannot target your tooltip但是除非您将鼠标悬停在 div 中,否则无法定位您的工具提示

DEMO演示

Try utilizing onShow event尝试使用 onShow 事件

$("#mydiv").tooltip({
            content: function () {
                return $(this).prop('title');
            },
           open: function (event, ui) {
               ui.tooltip.css("max-width", "800px")

           },
           position:{
                my:"left+10 top+25",
                at:"left top"
           },
           onShow: function() {
           $('#myspan').html()
           }
        });

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

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