How to get data attribute in ie 7?

2024/2/27 7:52:05

I use "data" attribute and in IE7 and I don't know how to get value of it. Can jQuery possibly help me?

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

<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> 
Answer

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);
});
http://en.ppmy.cn/q/41282.html

Related Q&A

How to clear sensitive memory in JavaScript?

I have a login form for a user to type his/her password. This form is bound to an AngularJS model. Suppose that in the corresponding controller the user-given password is available via $scope.password.…

How to clone an Iterator in javascript?

In ES6, is there any possible to clone an iterator states?var ma=[1,2,3,4]; var it=ma[Symbol.iterator](); it.next();if I want to remember here the it states how should I do in javascritp?what is reme…

Why doesnt IE8 handle iframe onload events?

Sample code: <!DOCTYPE html> <html> <head> <title></title> <script> function on_iframe_load() {document.getElementById(iframe_a).onload = function() {alert(Thanks f…

Streaming jquery(JS files) from a CDN (Google)

This one is a case of not doing your homework.:-) Apart from dynamic loading advantage, does it make sense to include a JavaScript library(jQuery in my case ) from a Google server when I can load it f…

How to check if current time falls within a specific range considering also minutes

I am doing a Website for Restaurants Home Delivery ,depending on Restaurants Home Delivery Timings i need to enable / disable Order Now ButtonI have got startTime and End Time in 12 Hour format .This …

Number of days between two dates in ISO8601 date format

I want to do same thing as How do I get the number of days between two dates in JavaScript?but I want do the same on this date format: 2000-12-31.

change URL link with javascript without refresh

Is it possible to automatically change the url example.com/4000/title-2/#!4000 to example.com/4000/title-2 without to refresh the page ? Basically to remove "/#!4000" from the URL.Note that …

Simulating ajax POST call using Python Requests

Im doing a project where my parser steals gets data about every video on the specific site and save it to my database. I have accomplished everything except full link to the video which is hidden. Ther…

How does setTimeout prevent potential stackoverflow

An example : var list = readHugeList();var nextListItem = function() {var item = list.pop();if (item) {setTimeout( nextListItem, 0);// ^^^^^^^^ this line} };How does use of setTimeout prevent potential…

Why cant AngularJS view send data to this re-usable service?

An AngularJS app needs to re-use multiple custom objects. I am concerned about creating cluttered, redundant code that is hard to maintain, so I moved re-usable code to a service that can be concisely…