2012-11-20 24 views
0

我試圖從鏈接中刪除title屬性重複使用該工具提示的工作,這裏是一個的給我找麻煩的代碼片段:removeAttr擺脫稱號不是在IE

$('.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中的鏈接,原來的提示重新出現。任何解決方案?謝謝!

+0

你使用什麼工具提示插件(如果有的話)?他們中的很多人爲你照顧這個。也許你可以看看其中的一個靈感。 –

+0

我通過閱讀書中的jQuery,新手忍者來了解它。它也在他們的例子中被打破。我希望有一個解決這個問題。 – Richard

回答

2

您能夠更改 冠軍 ATTR比 冠軍其他的東西嗎?我相信 標題是一個保留字,可能會導致一些問題。

- 工作:(用下面的代碼更新,現在的作品)

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'); 
});​ 

更新:

事實上,它仔細一看,你可以使用冠軍但在這種特定情況下它可能是更好地訪問它的$。數據()之外。也許是這樣的:

var $this = $(this); 
var t = $this.attr('title'); 
$this 
    .data('title', t) 
    .removeAttr('title'); 
  • 更新了 「非工作」 的jsfiddle代碼來反映以上。
+0

那麼,我們的目標是檢索標題屬性中的任何內容,並設計它的樣式,但從它聽起來像,它不是這樣做的最好方法。 – Richard

+0

非常好,感謝您的更新!完美的作品! – Richard