开发者

Remove title tag tooltip

开发者 https://www.devze.com 2023-03-19 00:42 出处:网络
Is there any way to remove the tooltip from title attribute without actually remove the title. I h开发者_运维知识库ave a link with a title attribute like this

Is there any way to remove the tooltip from title attribute without actually remove the title.

I h开发者_运维知识库ave a link with a title attribute like this

<a href="url" title="anotherURL"></a>

It is important that the title is intact since I need to read the url from there. All the fixes for this that I have found is to remove the title attribute and reuse it but in this case this is not possible.

Any ideas?


It's all about the browser. It's the browser that sees the title as a tooltip, from the browser specifications and interpretations.

You should use if you want to handle data like that, the HTML5 way (which you can use in any other document type as it's ignored) and use:

<a href="url" data-title="anotherURL"></a>

with the data- attributes, there will be no tooltip as title is not used, and you can easily get that using:

$("a").attr("data-title")

but, you will need to convert stuff and you said that you don't/can't do that.

you can easily convert all titles into data-title and clean the title using

$("a").attr("data-title", function() { return $(this).attr("title"); } );
$("a").removeAttr("title");

(all code is to be used with jQuery Framework)


As you didn't mark this question as jquery, I'm assuming that you'd be open to a pure JavaScript solution?

The following works (on Ubuntu 11.04) in Firefox 5, Chromium 12 and Opera 11, I'm unable to test in IE, but as I'm using querySelectorAll() I'd suspect that it wouldn't work well, if at all. However:

var titled = document.querySelectorAll('[title]'); // gets all elements with a 'title' attribute, as long as the browser supports the css attribute-selector
var numTitled = titled.length;

for (i=0; i<numTitled; i++){
    titled[i].setAttribute('data-title',titled[i].title); // copies from 'title' to 'data-title' attribute
    titled[i].removeAttribute('title'); // removes the 'title' attribute
}

JS Fiddle demo.


References:

  • document.querySelectorAll at the Mozilla Developer Network.


Why don't you use jQuery to move this information from title to element data.

Run this on element load:

$(el).data('url', $(el).attr('title')).attr('title', '');

And afterwards read URL like this:

$(el).data('url');

Variable el here is DOM element or element selector.

0

精彩评论

暂无评论...
验证码 换一张
取 消