Hide native tooltip using jQuery

后端 未结 11 818
南旧
南旧 2020-12-01 05:00

Is there a way to hide the native tooltip action when a user hovers over a anchor tag with a title attribute? I don\'t want to remove it just don\'t display the nasty yellow

11条回答
  •  臣服心动
    2020-12-01 05:32

    The original poster only wanted to disable the native action of .tooltip(). If that is the case, use the following simple solution:

    $(function() {
        $( document ).tooltip({
            items: "[data-tooltip]",
            content: function() {
                var element = $( this );
                if ( element.is( "[data-tooltip]" ) ) {
                    return element.attr('data-tooltip');
                }
            }
        });
    });
    

    Now the [title] attribute is disabled and the tooltip will only trigger when an element has a [data-tooltip] attribute. By defining more 'items' you can create different behavior and styles:

    $(function() {
        $( document ).tooltip({
            items: "[data-tooltip],img[alt]",
            content: function() {
                var element = $( this );
                if ( element.is( "[data-tooltip]" ) ) {
                    return element.attr('data-tooltip');
                }
                if ( element.is( "[alt]" ) ) {
                    return element.attr('alt') + something_else;
                }
            }
        });
    });
    

    http://jqueryui.com/tooltip/#custom-content & http://api.jqueryui.com/tooltip/#option-items

提交回复
热议问题