jQuery 1.4.4: How to find an element based on its data-attribute value?

不羁岁月 提交于 2019-12-04 06:51:40

data adds items to jQuery's internal data holder, not to the data- attributes. These are read into jQuery's data() structure, but values inserted using jQuery are not fed back into the DOM.

The easiest way to mimic this would be using .filter():

// To replicate $('.gallery li[data-slide]')
$('.gallery li').filter(function(){
    return (undefined !== $(this).data('slide'));
});

You could also do this as a custom selector:

$.expr[':'].hasData = function(obj, index, meta, stack) {
    return (undefined !== $(obj).data(meta[3]));
};

$('.gallery li:hasData(slide)'); // li elements under .gallery with "slide" data set
$(':hasData(slide)'); // any element with "slide" data set
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!