通过jQuery中的HTML5数据属性选择元素 [英] Select elements by HTML5 data attribute in jQuery

查看:131
本文介绍了通过jQuery中的HTML5数据属性选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过它们的HTML5在jQuery中选择元素 data 属性(例如,所有< div> ; 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.

这篇关于通过jQuery中的HTML5数据属性选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆