Changing the title attribute using jQuery while hovering over the element

北城以北 提交于 2019-12-05 10:12:09

The widget removes the text from the title attribute to prevent the native browser tooltip from appearing. When it removes the text, it stores it in data attached to the element.

You can do the same using this line of code:

$("#my-element").data("ui-tooltip-title", "My new tooltip message");

Now, if the user moves their mouse away from the element, and then hovers back onto it, it will show the new text.

To show the new text immediately, you need to update the live tooltip overlay element, which thankfully is easy to find. You just need to do this after you've updated the text:

$(".ui-tooltip-content").html($("#my-element").data("ui-tooltip-title"));

Tested on jQuery UI 1.10.0.

When using the tooltip widget, you can update the contents via its own API:

    $(this).tooltip('option', 'content', "New Content Goes Here");

Try this:


$('document').ready(function() { $( 'document' ).tooltip(); $(".btn").click(function(){ alert($(this).attr("title")); if ($(this).attr("title")=="T1"){ $(this).attr("title","T2"); }else{ $(this).attr("title","T1"); } }); });

$( 'document' ) should be in single or double quotes(' or ") and it works fine for me whitout $( 'document' ).tooltip();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!