我正在尝试从链接中删除标题属性并将其重新用于工具提示,这是给我带来麻烦的代码片段:
$('.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 是保留字,可能会导致一些问题。
非-工作:(使用下面的代码更新,现在可以工作)
<a href="#" title="foo" class="location">Test Link</a>
$('.location').each(function() {
var $this = $(this);
$this
.data('title', $this.attr('title'))
.removeAttr('title');
});
工作:
<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/