通过jQuery中的HTML5数据属性选择元素

通过jQuery中的HTML5数据属性选择元素

问题描述:

是否有可能通过它们的HTML5在jQuery中选择元素 data 属性(例如,所有< div&gt ; $ c> with data-role ='footer')?

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

使用一样在数据 - 属性上进行选择/ category / selectors / attribute-selectors /rel =noreferrer>属性选择器。在这种情况下,您需要属性等于选择器,如下所示:

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']")

它们由jQuery专门在消费中处理,例如允许 .data() 从中抓取使用正确的输入法......但只要DOM遍历,它们只是另一个属性,因此在编写选择器时应将其看作是这样。

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.