Javascript 如何向Fancybox 2中打开的图像添加html数据属性?
我希望打开的图像具有属性Javascript 如何向Fancybox 2中打开的图像添加html数据属性?,javascript,jquery,fancybox,fancybox-2,Javascript,Jquery,Fancybox,Fancybox 2,我希望打开的图像具有属性data pin=“true” 以下是我希望打开的图像的html格式: <img data-pin="true" class="fancybox-image" src="/uploads/gallery_item/image/426/full_ExShow2013_3156.jpg#lightbox0" alt="" > 编辑 缩略图的HTML格式: <a class="fancybox" href="/uploads/gallery_item/ima
data pin=“true”
以下是我希望打开的图像的html格式:
<img data-pin="true" class="fancybox-image" src="/uploads/gallery_item/image/426/full_ExShow2013_3156.jpg#lightbox0" alt="" >
编辑
缩略图的HTML格式:
<a class="fancybox" href="/uploads/gallery_item/image/426/full_example.jpg" rel="gallery">
<img alt="" data-src="/uploads/gallery_item/image/426/example.jpg" src="/uploads/gallery_item/image/426/example.jpg" style="width: 301px; height: 301px;"></div>
</a>
仅仅因为您的img
没有XML属性并不意味着它没有数据
通常
$(something).data("key", value);
不会将实际的数据键
属性添加到XML元素中,但它们将存储在DOM元素本身中
如果要显式添加标记属性,需要通过$.attr
执行此操作
$(something).attr("data-key", value);
您应该以打开图像的选择器为目标,如:
$(".fancybox").fancybox({
beforeShow: function () {
$(".fancybox-image").attr("data-pin", true)
}
});
请参见
注意:请记住,当您使用$(this.element)
时,您实际上指的是触发fancybox的链接,而不是open元素本身。您可以发布HTML吗?@APAD1这就是您要找的吗?尽管您的方法是正确的,我仍然想知道XML
与这个问题有什么关系
$(".fancybox").fancybox({
beforeShow: function () {
$(".fancybox-image").attr("data-pin", true)
}
});