简体   繁体   English

如何使用jQuery在浏览器中禁用工具提示?

[英]How to disable tooltip in the browser with jQuery?

Is there a way to disable browser tooltip from displaying when hovering over elements that have attribute 'title' populated? 当将鼠标悬停在已填充属性“title”的元素上时,有没有办法禁用浏览器工具提示? Note that I don't want to remove title content. 请注意,我不想删除标题内容。 Here is the code are requested: 这是代码请求:

  $(document).ready(function() {
     $('a.clickableSticky').cluetip({
         splitTitle: '|',
         showTitle: false,
         titleAttribute: 'description',
         activation: 'click',
         sticky: true,
         arrows: true,
         closePosition: 'title'
     });
 });

and in asp.net 并在asp.net

  <ItemTemplate>
     <a class="clickableSticky" href="#"
     title=' <%#((Limit)Container.DataItem).Tip %>'>
     <img src="..\App_Themes\default\images\icons\information.png"/>
     </a>

 </ItemTemplate>

You could remove the title attribute on page load. 您可以在页面加载时删除title属性。

$(document).ready(function() {
    $('[title]').removeAttr('title');
});

If you need to use the title later, you can store it in the element's jQuery data() . 如果以后需要使用标题,可以将其存储在元素的jQuery data()

$(document).ready(function() {
    $('[title]').each(function() {
        $this = $(this);
        $.data(this, 'title', $this.attr('title'));
        $this.removeAttr('title');
    });
});

Another option is to change the name of the title attribute to aTitle , or something else that the browser would ignore, and then update any JavaScript to read the new attribute name instead of title . 另一个选项是将title属性的名称更改为aTitle ,或浏览器将忽略的其他内容,然后更新任何JavaScript以读取新属性名称而不是title

Update: 更新:

An interesting idea you could use is to "lazily" remove the title when hovering over an element. 您可以使用的一个有趣的想法是“悬停”在悬停在元素上时删除标题。 When the user hovers off the element, you can then put the title value back. 当用户将元素悬停时,您可以返回标题值。

This isn't as straightforward as it should be because IE doesn't correctly remove the tooltip on the hover event if you set the title attribute to null or remove the title attribute. 这并不像应该的那样简单,因为如果将title属性设置为null或删除title属性,IE不会正确删除悬停事件上的工具提示。 However, if you set the tooltip to an empty string ( "" ) on hover, it will remove the tooltip from all browsers including Internet Explorer. 但是,如果在悬停时将工具提示设置为空字符串( "" ),它将从包括Internet Explorer在内的所有浏览器中删除工具提示。

You can use the method I mentioned above to store the title attribute in jQuery's data(...) method and then put it back on mouseout . 您可以使用我上面提到的方法将title属性存储在jQuery的data(...)方法中,然后将其放回mouseout

$(document).ready(function() {
    $('[title]').mouseover(function () {
        $this = $(this);
        $this.data('title', $this.attr('title'));
        // Using null here wouldn't work in IE, but empty string will work just fine.
        $this.attr('title', '');
    }).mouseout(function () {
        $this = $(this);
        $this.attr('title', $this.data('title'));
    });
});

Here is the modern jQuery way to do it (IMO)... 这是现代 jQuery方法(IMO)......

$('[title]').attr('title', function(i, title) {
    $(this).data('title', title).removeAttr('title');
});

...and of course, reading the title attribute back is done with... ...当然,阅读title属性是用...完成的

$('#whatever').data('title');

Following Dan Herbert's suggestion above, here is the code: 遵循Dan Herbert的上述建议,以下是代码:

$(element).hover(
    function () {
        $(this).data('title', $(this).attr('title'));
        $(this).removeAttr('title');
    },
    function () {
        $(this).attr('title', $(this).data('title'));
    });

You could use jQuery to remove the contents of the title attribte, or move it into some other parameter for later use. 您可以使用jQuery删除标题attribte的内容,或将其移动到其他参数中供以后使用。

This does mean you lose some accessibility though. 这确实意味着你失去了一些可访问性。

RE: ClueTip A search of Google seems to suggest this is a common problem - is this only happening in IE? RE:ClueTip Google的搜索似乎表明这是一个常见问题 - 这只发生在IE中吗? ClueTip seems to work as expected in FireFox. ClueTip似乎在FireFox中按预期工作。

function hideTips(event) {  
    var saveAlt = $(this).attr('alt');
    var saveTitle = $(this).attr('title');
    if (event.type == 'mouseenter') {
        $(this).attr('title','');
        $(this).attr('alt','');
    } else {
        if (event.type == 'mouseleave'){
            $(this).attr('alt',saveAlt);
            $(this).attr('title',saveTitle);
        }
   }
}

$(document).ready(function(){
 $("a").live("hover", hideTips);
});

Hi all. 大家好。 I am using this solution and it works fine in all browsers. 我正在使用此解决方案,它适用于所有浏览器。

破解ClueTip以使用重命名的title属性。

As I need this functionality to be available during another action (as copy screen or pick color,) my solution contains two functions to be called when that action starts and when it ends: 由于我需要在另一个操作(如复制屏幕或选择颜色)中使用此功能,因此我的解决方案包含两个在该操作开始时和结束时调用的函数:

$.removeTitles = function() {
    $('[title]').bind('mousemove.hideTooltips', function () {
    $this = $(this);
    if($this.attr('title') && $this.attr('title') != '') { 
        $this.data('title', $this.attr('title')).attr('title', '');
    }
  }).bind('mouseout.hideTooltips', function () {
    $this = $(this);
    $this.attr('title', $this.data('title'));
  });
}

$.restoreTitles = function() {
    $('[title]').unbind('mousemove.hideTooltips').unbind('mouseout.hideTooltips');
    $('*[data-title!=undefined]').attr('title', $this.data('title'));
}

MouseEnter cannot be used because other element(s) could lay over the titled element (as an image in a titled div) and than mouseOut will occur as soon as hover over the inner element (the image!) 无法使用MouseEnter,因为其他元素可以覆盖标题元素(作为标题div中的图像),并且只要将鼠标悬停在内部元素(图像!)上就会出现mouseOut

However when using mouseMove you should pay attention because the event fires multiple times. 但是,当使用mouseMove时,您应该注意,因为事件会多次触发。 To avoid wiping the saved data, check first that the title is not empty! 为避免擦除保存的数据,请先检查标题是否为空!

The last line in RemoveTitles, attempts to restore the title from the element from which the mouse did not exit when calling the end on mouseClick or on a shortcut key. RemoveTitles中的最后一行,尝试从鼠标单击或快捷键上调用结束时鼠标未退出的元素恢复标题。

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

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