• Geek asks and answers

    JQuery UI: data selector does not return HTML5 data attributes

    Say I have this piece of HTML:

    <div id="container">
       <ul>
           <li><a href="#" data-some-info="foo">Bar</a>
           <!-- repeating a few like the one -->                          -->
           <!-- above for a while ...        -->
       </uL>
    </div>
    

    And I want to apply something to all data binding elements some-infowhen this is defined.

    I just want to understand why this behavior (is this a mistake?)

    $("#container").find("a:data(some-info)").size();  // -> 0
    $("#container").find("a").data("some-info");       // -> "foo"
    $("#container").find("a:data(some-info)").size();  // -> 1 (actual expected val)
    

    Any ideas?

    Additional info : I am using jQuery 1.9.0 and jQuery UI 1.9.2

    ** Change **

    I know about $("#container").find("a[data-some-info]"). This is not what I am asking here. If there is a data selector , why does it behave this way?

    +5
    jquery jquery-ui selector
    Yanick rochon Jan 30 '13 at 19:06
    source share
    3 answers

    JQuery. , JQuery , HTML5 JQuery ( .data), :data ( JQuery HTML5)

    :

    $("#container").find("a[data-some-info='foo']"), -, .find("a[data-some-info]").

    +1
    Plynx 30 . '13 19:10

    :

    $.extend( $.expr[ ":" ], {
        data: $.expr.createPseudo ?
            $.expr.createPseudo(function( dataName ) {
                return function( elem ) {
                    return !!$.data( elem, dataName );
                };
            }) :
            // support: jQuery <1.8
            function( elem, i, match ) {
                return !!$.data( elem, match[ 3 ] );
            },
    

    (https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.core.js#L160)

    , $.data, $(elem).data. , , , $.data $().data. , , . - , . [data-foo] , jQuery $().data, , jQuery .

    +3
    Kevin B 30 . '13 19:22

    $( "div:data(foo)") a <div>, , .data( "foo", value )

    jquery →

    jQuery data() , ( jQuery)

    https://github.com/jquery/jquery/blob/master/src/data.js#L51

    https://github.com/jquery/jquery/blob/master/src/data.js#L63

    HTML, , jQuery :data.

    , .data(some-info), , :data , .

    NOT , .


    .length, .

    .size , jQuery 1.8. http://api.jquery.com/size/

    +1
    Selvakumar Arumugam 30 . '13 19:07

    More articles:

    • Creating a simple HTTP proxy in C - c
    • How to adjust distance between y-mark and y-axis in Matlab? - matlab
    • Why does this flash admin form always fail verification? - python
    • PHP: создать миниатюру с обрезанным прямоугольником - php
    • How does PHP internally use keys and array types? - php
    • Common pre-C89 compilers / stdlib idioms (1985-1988) - c
    • JQuery DataTable Keydown - jquery
    • How to disable the download button while downloading and adding a file - javascript
    • cannot get singleton session in EcomDev test PHPUnit - unit-testing
    • ASP.NET Web API does not return XML - json

    All Articles

    Geek-Ask | 2020