简体   繁体   English

使用 javascript/jquery 在鼠标悬停时隐藏链接的标题属性

[英]hide title attribute from link on mouse hover with javascript/jquery

I am using the title attribute in all my links but I don't want to be visible on mouse hover, but still visible in the code for screen readers.我在所有链接中都使用了 title 属性,但我不想在鼠标悬停时可见,但在屏幕阅读器的代码中仍然可见。

var links = document.getElementsByTagName('a');

for (var i = 0; i < links.length; i++) {
    if (links[i].className == 'suppress') {
    links[i]._title = links[i].title;
    links[i].onmouseover = function() { 
    this.title = '';
}

links[i].onmouseout = function() { 
    this.title = this._title;
}

Using jQuery, you can hide the title attr on hover, and replace it on mouseout:使用 jQuery,您可以在悬停时隐藏title属性,并在鼠标悬停时替换它:

$(function(){

    $('a').hover(function(e){

        $(this).attr('data-title', $(this).attr('title'));
        $(this).removeAttr('title');

    },
    function(e){

        $(this).attr('title', $(this).attr('data-title'));

    });​

});

Like in this fiddle .就像在这个小提琴中一样

Since you are using jQuery, you can do it in a simple way:由于您使用的是 jQuery,您可以通过一种简单的方式进行操作:

$(document).ready(function(){
    $("a").removeAttr("title");
});

Or, setting it to empty value:或者,将其设置为空值:

$(document).ready(function(){
    $("a").attr("title", "");
});

If this is gonna change the way the screen readers read, then, you can just target on hover.如果这会改变屏幕阅读器的阅读方式,那么您可以将鼠标悬停在鼠标上。

$(document).ready(function(){
    $("a").hover(function(){
        $(this).attr("rel", $(this).attr("title"));
        $(this).removeAttr("title");
    }, function(){
        $(this).attr("title", $(this).attr("rel"));
        $(this).removeAttr("rel");
    });
});

I was trying to create a bubble tool-tip in CSS but the browser tool-tip would always appear and mess things up.我试图在 CSS 中创建一个气泡工具提示,但浏览器工具提示总是会出现并把事情搞砸。 So, like you, I needed to disable the default tool-tip.所以,和你一样,我需要禁用默认的工具提示。

I used a bit of JQuery to remove the "Title" tag but only on mouse hover.我使用了一些 JQuery 来删除“标题”标签,但只在鼠标悬停时。 As soon as the mouse is out, the "Title" tag is restored.一旦鼠标离开,“标题”标签就会恢复。

Following is a DIV with some "Title" content:以下是带有一些“标题”内容的 DIV:

<div class="tooltip-class" title="This is some information for our tooltip.">
  This is a test
</div>

Now you can run the following JQuery to hide the Title tag on mouse hover:现在您可以运行以下 JQuery 来隐藏鼠标悬停时的 Title 标签:

$(document).ready(function(){
  $(".tooltip-class").hover(function(){
    $(this).attr("tooltip-data", $(this).attr("title"));
    $(this).removeAttr("title");
  }, function(){
    $(this).attr("title", $(this).attr("tooltip-data"));
    $(this).removeAttr("tooltip-data");
  });
});

Following is a link to the full example:以下是完整示例的链接:

http://jsfiddle.net/eliasb/emG6E/54/ http://jsfiddle.net/eliasb/emG6E/54/

$(".element").hover(function(){ $(".element").hover(function(){

    // Get the current title
    var title = $(this).attr("title");

    // Store it in a temporary attribute
    $(this).attr("tmp_title", title);

    // Set the title to nothing so we don't see the tooltips
    $(this).attr("title","");

});

$(".element").click(function(){// Fired when we leave the element $(".element").click(function(){// 当我们离开元素时触发

    // Retrieve the title from the temporary attribute
    var title = $(this).attr("tmp_title");

    // Return the title to what it was
    $(this).attr("title", title);

});

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

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