开发者

Reading jQuery Data Value

I need to disable the title value, so the hover doesn't show up, and still read its value. According to this StackOverflow question, the following 开发者_Go百科should work:

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

Which does remove the title property, the only problem is, I can't for the life of me figure out how to read that data value. I know it's a simple question, but I'd really appreciate the help, the jQuery documentation didn't help me on this.

What I currently have is: var description = $(this).find("img").data(this, "title"); which doesn't work for some reason.


The code example you gave could be tidied up a bit with the latest jQuery versions...

You probably want something like this...

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

This will assign the title attribute to the data store of each element with a title attribute, and then remove the title attribute of the element.

Then you can read an element's old title attribute with...

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

jsFiddle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜