jQuery: Select data attributes that aren't emp

2019-01-08 09:44发布

I'm trying to select all elements that have a data-go-to attribute that is not empty.

I've tried $('[data-go-to!=""]') but oddly enough it seems to be selecting every single element on the page if I do that.

11条回答
Rolldiameter
2楼-- · 2019-01-08 10:06

This works for me

$('[attributename]').filter('[attributename!=""]')
查看更多
相关推荐>>
3楼-- · 2019-01-08 10:10

Try this :

$('[data-go-to:not(:empty)]')
查看更多
我命由我不由天
4楼-- · 2019-01-08 10:14
$('[data-go-to]').filter(function() {
    return $(this).data('go-to')!="";
});

Using :not, .not(), :empty etc will only check if the element itself is empty, not the data attribute. For that you will have to filter based on the data attributes value.

FIDDLE

查看更多
欢心
5楼-- · 2019-01-08 10:15

This works for me:

$('.data .title td[data-field!=""]')
查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-08 10:17

I'm not sure about a simple selector, but you could use filter():

$('[data-go-to]').filter(
    function(){
        return ($(this).attr('data-go-to').length > 0);
    });

JS Fiddle demo.

References:

查看更多
冷血范
7楼-- · 2019-01-08 10:17

Has 'data-attributename' and its value is not empty:

$('[data-attributename]:not([data-attributename=""])')

Has 'data-attributename' empty or not:

$('[data-attributename]')

JS Fiddle example

查看更多
登录 后发表回答