How to get “data” attribute in ie 7?

三世轮回 提交于 2019-12-01 05:38:18

jQuery can help ... the data attribute works with the data() function in jQuery.

$(srcElement).data('pk');

You can use it with any data attribute, for example, if you had:

<div id="DivId" data-something="foo" data-somethingElse="bar">

You can get the data out by:

$('#DivId').data('something');
$('#DivId').data('somethingElse');

To set data:

$('#DivId').data('something', 'foo');
$('#DivId').data('somethingElse', 'bar');

Here is a link to jQuery .data()

EDIT:

I think you want:

$('.image').click(function () {
    openSlide($(this).data('pk'), false);
});

The top answer is outdated. Given the example <div id="DivId" data-somethingElse="bar"></div>, you will have to do $('#DivId').data('somethingelse') to get the data. The better standard way is to use snake case in HTML, which will yield camelcase in JavaScript:

HTML:

<div id="foo" data-something-else="bar"></div>

JS:

alert($('#foo').data('somethingElse')); // Alerts "bar"

You can use jQuerys (or other libs) attr getter. Or you can look into jQuery source and find out how they achieved cross-browser consistency.

In jQuery you can use the data method, like this:

$(srcElement).data('pk');

Note that the string to pass into data is just 'pk', you simply drop the 'data-' part of the attribute.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!