Hide native tooltip using jQuery

后端 未结 11 782
南旧
南旧 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:07
    var tempTitle = "";
    $('a[title]').hover(
        function(e){
             e.preventDefault();
             tempTitle = $(this).attr('title');
    
             $(this).attr('title', '');
    
                 $(this).mousedown(
                    function(){
                        $(this).attr('title', tempTitle);
                    }
                );
        }
       ,
       function() {
           $(this).attr('title', tempTitle);
       }
    );
    

    Try it works like a dog!

    0 讨论(0)
  • 2020-12-01 05:14

    You can remove it by:

    $("a").removeAttr("title");
    

    This will remove it for js-users only, so it's still accessable and findable for search engines.

    0 讨论(0)
  • 2020-12-01 05:18

    Its works like this:

    Rename to sTitle instead of default title attribute and if you need to call it from Jquery:

    getAttribute('stitle')

    It works on all.

    0 讨论(0)
  • 2020-12-01 05:21

    To get it out of the title, I would use the data() method:

    $(document).ready( function () {
        $('.items_with_title').each( function () {
            $(this).data('title', $(this).attr('title') );
            $(this).removeAttr('title');
        });
    });
    
    // to access it
    $(document).ready( function () {
        $('A').click( function () {
            alert($(this).data('title'));
        });
    });
    

    You could also make the selector for any item that has a title attribute:

    $('*[title]').each(...
    
    0 讨论(0)
  • 2020-12-01 05:23

    I know this is post about Jquery but I just ran in to this issue and is mostly connected with lighboxes so here is Mootools fix for iaian7 Mediabox Advanced on image links if anyone needs it The fix will work on any of these also http://line25.com/articles/rounding-up-the-top-10-mootools-lightbox-scripts

    if ($$('.lbThumb').length > 0) { //  lbThumb a class or what ever you are using
        $$('.lbThumb').each(function (el) { // same here , your a class
    
            var savedtitle = el.get('title');
            var getimage  = el.getElement('img'); 
                        // must use image click since Mediabox will kill our a element click
            getimage.addEvent('click', function () {
                el.set('title',savedtitle );
            });
            // hide nasty a tooltip 
            el.addEvents({
            mouseenter: function () {
              el.erase('title');
            },
            // bring it back 
            mouseleave: function () {
              el.set('title',savedtitle );
    
            }
          });
    
       });
    }
    
    0 讨论(0)
  • 2020-12-01 05:23

    Here's another spin-off that you might find useful, in case you use a lightbox JS plugin that still needs the "title" attribute for title processing on the lightbox slides:

    $('a.lightbox-trigger').each(function() { // Process all lightbox trigger links
    
        $(this).mouseover(function() {
            if(!$(this).data('keep'))  // 1st time = FALSE, so make the swap
                $(this).attr('data-title', $(this).attr('title')).attr('title', '');
            $(this).data('keep', true); // Preserve value if hovered before clicked
        });
    
        $(this).mousedown(function() {
            $(this).attr('title', $(this).attr('data-title')).attr('data-title', '');
            $(this).data('keep', false); // Mark element as safe to swap again
        });
    });
    
    0 讨论(0)
提交回复
热议问题