2011-09-04 84 views
2

我需要禁用标题值,因此悬停显示不出来,而且还读它的价值。据this StackOverflow question,下面应该工作:阅读jQuery的数据值

$("[title]").each(function() { 
     $this = $(this); 
     $.data(this, "title", $this.attr("title")); 
     $this.removeAttr("title"); 
    }); 

哪个不去掉标题属性,唯一的问题是,我不能为我的生活弄清楚如何读取数据值。我知道这是一个简单的问题,但我真的很感谢帮助,jQuery文档在这方面没有帮助。

我目前拥有的是: var description = $(this).find("img").data(this, "title");不出于某种原因。

回答

2

你给的代码示例可以收拾了一下与最新的jQuery版本...

你可能想是这样的......

$('[title]').attr('title', function(i, title) { 
    $(this).data('title', title).removeAttr('title'); 
}); 

这将分配title属性将每个元素的数据存储与title属性相关联,然后删除该元素的title属性。

然后你就可以用阅读元素的老title属性...

$('.something').data('title'); 

jsFiddle

+0

谢谢你,这工作很棒:)我会在一分钟内接受它。 – JacobTheDev