jQuery: Select data attributes that aren't empty?

前端 未结 11 1105
死守一世寂寞
死守一世寂寞 2020-11-30 23:30

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 enou

相关标签:
11条回答
  • 2020-11-30 23:41

    According to the documentation this should do it

    :not([attr="value"])

    DEMO

    Hope this helps

    0 讨论(0)
  • 2020-11-30 23:49
    $('[data-go-to!=""]:[data-go-to]').each(function() {
        // Do Your Stuff
    });​
    
    0 讨论(0)
  • 2020-11-30 23:53

    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:

    • filter().
    0 讨论(0)
  • 2020-11-30 23:56
    $('[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

    0 讨论(0)
  • 2020-11-30 23:57

    This works for me

    $('[attributename]').filter('[attributename!=""]')
    
    0 讨论(0)
  • 2020-11-30 23:58
    $('[data-go-to]:not([data-go-to=""])')
    

    JSBIN

    0 讨论(0)
提交回复
热议问题