在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:20:47  来源:igfitidea点击:

Select elements by HTML5 data attribute in jQuery

jqueryhtml

提问by Diogo Cardoso

Is it possible to select elements in jQuery by their HTML5 dataattributes (for example, all <div>with data-role='footer')?

是否可以通过它们的 HTML5data属性(例如,全部<div>带有data-role='footer')来选择 jQuery 中的元素?

回答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')")

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

来源:http: //jquerymobile.com/test/docs/api/methods.html

回答by darthwade

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

jQuery 和 Zepto 集成:https: //github.com/kossnocorp/role