javascript - removeAttr 删除在 IE 中不起作用的标题

标签 javascript jquery tooltip

我正在尝试从链接中删除标题属性并将其重新用于工具提示,这是给我带来麻烦的代码片段:

 $('.location').each(function() {
    var $this = $(this);
    $this
      .data('title', $this.attr('title'))
      .removeAttr('title');
  });


$('.location').hover(function(e) {
    //hover over code   

    //grabs what's inside the title attribute in the html
    var titleText = $(this).data('title');

    //saves the tooltip text using the data method, so that the orignal tooltip text does not conflict
    $(this)
        .data('tipText', titleText)
        .removeAttr('title');

我在此处搜索时添加了以下代码:

   $('.location').each(function() {
    var $this = $(this);
    $this
      .data('title', $this.attr('title'))
      .removeAttr('title');
  });

这运行良好,但只有一次,如果我返回 IE8 中的链接,原始工具提示会重新出现。有什么解决办法吗?谢谢!

最佳答案

您是否可以将标题属性更改为标题以外的其他内容?我认为 title 是保留字,可能会导致一些问题。

-工作:(使用下面的代码更新,现在可以工作)

http://jsfiddle.net/abZ6j/3/

<a href="#" title="foo" class="location">Test Link</a>​

$('.location').each(function() {
    var $this = $(this);
    $this
        .data('title', $this.attr('title'))
        .removeAttr('title');
});​

工作:

http://jsfiddle.net/abZ6j/1/

<a href="#" linkTitle="foo" class="location">Test Link</a>​


$('.location').each(function() {
    var $this = $(this);
    $this
        .data('title', $this.attr('linkTitle'))
        .removeAttr('linkTitle');
});​

更新:

实际上,仔细观察,您可以使用 title,但在这个特定实例中,最好在 $.data() 之外访问它。也许是这样的:

var $this = $(this);
var t = $this.attr('title');
$this
  .data('title', t)
  .removeAttr('title');
  • 更新了“非工作”jsFiddle 代码以反射(reflect)上述情况。

关于javascript - removeAttr 删除在 IE 中不起作用的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13479886/

相关文章:

javascript - JQuery 可拖动 div

javascript - 使用 jQuery 隐藏 Bootstrap 选项卡直至 .active

jquery-ui - jQueryUI 工具提示小部件在单击时显示工具提示

c++ - 动态工具提示 : TTN_GETDISPINFO not send when using LPSTR_TEXTCALLBACK

javascript - 为什么 jQuery 的更改事件会多次触发?

javascript - 即使手动 URL 有效,网站也无法识别 GET

javascript - 悬停效果与 jQuery 影响容器 div

javascript - 在 Prototype/Scriptaculous 上带有 AJAX 拉取内容的可悬停工具提示(表现得像 Twitter 的)`

javascript - Firefox 扩展 : load script from chrome://url into page

javascript - 把这个数组和jQuery结合起来,一头雾水