javascript 如何在 ie 7 中获取“数据”属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7361356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get "data" attribute in ie 7?
提问by I159
I use "data" attribute and in IE7 and I don't know how to get value of it. Can jQuery possibly help me?
我在 IE7 中使用“数据”属性,但我不知道如何获得它的价值。jQuery 可以帮助我吗?
I have this
我有这个
window.event.srcElement.getAttribute('data-pk')
Of course it does not work.
当然这是行不通的。
Edit:
编辑:
for (i=0; i < max; i++) {
if (typeof attachEvent == 'undefined'){
//open[i].addEventListener('click', function(e){ openSlide(e.currentTarget.getAttribute('data-pk')), false});
open[i].onclick = function(e){ openSlide(e.currentTarget.getAttribute('data-pk')), false};
} else {
open[i].attachEvent('onclick', function(){
openSlide(window.event.srcElement.getAttribute('data-pk'))}, false);
};
};
html
html
<div>
<img class='image' data-pk='18' src='/site_media/media/img/120x180.jpg'>
<img class='image' data-pk='13' src='/site_media/media/img/007b-300x224.jpg'>
<img class='image' data-pk='15' src='/site_media/media/img/IMG_0549_1.jpg'>
</div>
回答by Martin
jQuery can help ... the data attribute works with the data() function in jQuery.
jQuery 可以提供帮助... data 属性与 jQuery 中的 data() 函数一起使用。
$(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()
这是jQuery .data()的链接
EDIT:
编辑:
I think you want:
我想你想要:
$('.image').click(function () {
openSlide($(this).data('pk'), false);
});
回答by Ezekiel Victor
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:
最佳答案已过时。鉴于示例<div id="DivId" data-somethingElse="bar"></div>
,您将不得不执行$('#DivId').data('somethingelse')
获取数据。更好的标准方法是在 HTML 中使用蛇形大小写,这将在 JavaScript 中产生驼峰式大小写:
HTML:
HTML:
<div id="foo" data-something-else="bar"></div>
JS:
JS:
alert($('#foo').data('somethingElse')); // Alerts "bar"
回答by nikmd23
In jQuery you can use the data method, like this:
在 jQuery 中,您可以使用 data 方法,如下所示:
$(srcElement).data('pk');
Note that the string to pass into data is just 'pk', you simply drop the 'data-' part of the attribute.
请注意,传递给数据的字符串只是“pk”,您只需删除属性的“data-”部分。
回答by Adam Jurczyk
You can use jQuerys (or other libs) attrgetter. Or you can look into jQuery sourceand find out how they achieved cross-browser consistency.
您可以使用 jQuery(或其他库)attrgetter。或者您可以查看jQuery 源代码并了解它们是如何实现跨浏览器一致性的。