在 jQuery 中通过 HTML5 数据属性选择元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4993447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select elements by HTML5 data attribute in jQuery
提问by Diogo Cardoso
回答by Nick Craver
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:
您可以data-
像任何其他属性一样选择属性...使用属性选择器。在这种情况下,您需要属性等于选择器,如下所示:
$("div[data-role='footer']")
They are handled specially in consumptionby 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.
它们被特殊处理消耗的jQuery,例如,允许.data()
从他们正确的打字取...但据DOM遍历推移,他们只是另一个属性,书写选择时这样认为他们这样。
回答by user113716
$('div[data-role="footer"]')
This simply uses the the attribute-equals-selector
(docs).
这只是使用attribute-equals-selector
(docs)。
There are several attribute selectorsyou can use (among the others).
有几个属性选择,你可以使用(除其他)。
回答by danpop
jQuery Mobile recomends the $.fn.jqmData(), for example : $("div:jqmData(role='footer')")
jQuery Mobile 推荐 $.fn.jqmData(),例如: $("div:jqmData(role='footer')")
回答by darthwade
jQuery & Zepto integration: https://github.com/kossnocorp/role
jQuery 和 Zepto 集成:https: //github.com/kossnocorp/role