Select elements by HTML5 data attribute in jQuery

后端 未结 4 693
闹比i
闹比i 2020-12-24 00:39

Is it possible to select elements in jQuery by their HTML5 data attributes (for example, all

with data-role=\'footer\')?

相关标签:
4条回答
  • 2020-12-24 01:19

    jQuery Mobile recomends the $.fn.jqmData(), for example : $("div:jqmData(role='footer')")

    Source: http://jquerymobile.com/test/docs/api/methods.html

    0 讨论(0)
  • 2020-12-24 01:19

    jQuery & Zepto integration: https://github.com/kossnocorp/role

    0 讨论(0)
  • 2020-12-24 01:24

    You can select on a data- attribute like any other attribute...using an attribute selector. In this case you want the attribute-equals selector, like this:

    $("div[data-role='footer']")
    

    They are handled specially in consumption by jQuery, e.g. allowing .data() to fetch from them with correct typing...but as far as DOM traversal goes, they're just another attribute, so think of them as such when writing selectors.

    0 讨论(0)
  • 2020-12-24 01:38
    $('div[data-role="footer"]')
    

    This simply uses the the attribute-equals-selector(docs).

    There are several attribute selectors you can use (among the others).

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