Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向Fancybox 2中打开的图像添加html数据属性?_Javascript_Jquery_Fancybox_Fancybox 2 - Fatal编程技术网

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)
    }
});