[英]Set data-attribute if contains image as child
To get my lightbox working, I am trying to set the data attribute "lightbox" on links that contain an image as a child. 为了使我的灯箱正常工作,我试图在包含作为子图像的链接上设置数据属性“灯箱”。 This doesn't seem to work too well.
这似乎不太好用。 I have this fiddle .
我有这个小提琴 。
var links = $(".single .entry-content a");
links.each(function () {
if ($(this).children("img").length > 0) {
$(this).data("lightbox","image-gallery");
console.log("test");
}
});
The console logs, but the data-attribute isn't set. 控制台将记录日志,但未设置数据属性。 What did I do wrong?
我做错了什么?
jQuery's .data()
function does not add data attributes, but instead uses it's own internal storage mechanism. jQuery的
.data()
函数不添加数据属性,而是使用它自己的内部存储机制。 To actually change the attribute, you'll need to use attr()
: 要实际更改属性,您需要使用
attr()
:
$('.single .entry-content a').has('> img').attr('data-lightbox', 'image-gallery');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.