admin管理员组文章数量:1279213
I am trying to retrieve a data value which contains an image url when the user clicks on a thumbnail image. HTML is as follows:
<div class="port_image_holder">
<a class="port_enlarge" href="javascript:void(0)">
<img class="lazy" src="html_includes/include_images/image_loading.jpg"
data-original="html_includes/include_images/test-image.png"
data-large="html_includes/include_images/test-image-large.png"
alt="Caption Goes Here.." />
<noscript>
<img src="html_includes/include_images/test-image.png"
data-large="html_includes/include_images/test-image-large.png"
alt="" />
</noscript>
</a>
</div>
Basically when the user clicks on the image I need to obtain the url set in the data-large
attribute.
Currently I can get the url from the src
using the find()
method:
var img_url=$(this).find('img').attr('src');
but so far have had no luck getting the data reference. Please note that there are numerous port_image_holder
classes on the page as these are looped out as required.
I am trying to retrieve a data value which contains an image url when the user clicks on a thumbnail image. HTML is as follows:
<div class="port_image_holder">
<a class="port_enlarge" href="javascript:void(0)">
<img class="lazy" src="html_includes/include_images/image_loading.jpg"
data-original="html_includes/include_images/test-image.png"
data-large="html_includes/include_images/test-image-large.png"
alt="Caption Goes Here.." />
<noscript>
<img src="html_includes/include_images/test-image.png"
data-large="html_includes/include_images/test-image-large.png"
alt="" />
</noscript>
</a>
</div>
Basically when the user clicks on the image I need to obtain the url set in the data-large
attribute.
Currently I can get the url from the src
using the find()
method:
var img_url=$(this).find('img').attr('src');
but so far have had no luck getting the data reference. Please note that there are numerous port_image_holder
classes on the page as these are looped out as required.
-
2
not this:
var img_url=$(this).find('img').attr('data-large');
? – Ivan Chernykh Commented Jun 10, 2013 at 7:52 - haha - sometimes the answers are so obvious - thanks :) – Sideshow Commented Jun 10, 2013 at 7:53
- or what about using $(this).find('img').data('large') ? – halilb Commented Jun 10, 2013 at 7:54
- Thanks @halib - tried this but for some reason did not work - just tried it again and it did ! Perhaps I had a typo in my original attempt. – Sideshow Commented Jun 10, 2013 at 7:57
1 Answer
Reset to default 9You can find data-large
attribute on the image using .data()
method like:
var img_url = $(this).find('img').data('large');
// Check the console for url
console.log(img_url);
本文标签: javascriptHow do I find data* attribute value on click in jQueryStack Overflow
版权声明:本文标题:javascript - How do I find data-* attribute value on click in jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741273155a2369577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论