删除标题标记工具提示

Tob*_*ias 4 html javascript css tooltip title

有没有办法从title属性中删除工具提示而不实际删除标题.

我有一个像这样的title属性的链接

<a href="url" title="anotherURL"></a>
Run Code Online (Sandbox Code Playgroud)

重要的是标题是完整的,因为我需要从那里读取网址.我找到的所有修复程序都是删除title属性并重用它,但在这种情况下,这是不可能的.

有任何想法吗?

bal*_*dre 11

这都是关于浏览器的.浏览器title从浏览器规范和解释中将其视为工具提示.

你应该使用,如果你想处理这样的数据,HTML5方式(你可以在任何其他文档类型中使用,因为它被忽略)并使用:

<a href="url" data-title="anotherURL"></a>
Run Code Online (Sandbox Code Playgroud)

使用data-属性,将没有title未使用的工具提示,您可以使用以下方法轻松获得:

$("a").attr("data-title")
Run Code Online (Sandbox Code Playgroud)

但是,你需要转换东西,你说你不能/不能这样做.

您可以轻松地将所有内容转换titledata-title清除标题并使用

$("a").attr("data-title", function() { return $(this).attr("title"); } );
$("a").removeAttr("title");
Run Code Online (Sandbox Code Playgroud)

(所有代码都将与jQuery Framework一起使用)


Dav*_*mas 5

由于您没有将此问题标记为,我假设您愿意接受纯 JavaScript 解决方案?

以下工作(在 Ubuntu 11.04 上)在 Firefox 5、Chromium 12 和 Opera 11 中,我无法在 IE 中进行测试,但是当我使用时,querySelectorAll()我怀疑它无法正常工作,如果有的话。然而:

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
}
Run Code Online (Sandbox Code Playgroud)

JS小提琴演示


参考:


Ged*_*rox 0

为什么不使用 jQuery 将此信息从 移动title到 element data

在元素加载上运行:

$(el).data('url', $(el).attr('title')).attr('title', '');
Run Code Online (Sandbox Code Playgroud)

然后读取如下 URL:

$(el).data('url');
Run Code Online (Sandbox Code Playgroud)

这里的变量el是 DOM 元素或元素选择器。