简体   繁体   English

在jQuery选择器中使用HTML5数据属性

[英]Using HTML5 Data Attribute in jQuery Selector

Noob Question on Data Attribute Noob关于数据属性的问题

I was wondering will using data-attribute in jQuery Selector can bring any trouble in the future? 我想知道在jQuery Selector中使用数据属性会在将来带来任何麻烦吗?

I'm trying to reduced the usage of .class and #id as jQuery Selector, since most of data I'm working on will generated from data-attribute 我正在尝试减少.class和#id作为jQuery Selector的使用,因为我正在处理的大部分数据都是从data-attribute生成的

example of the code 代码示例

$(document).ready(function(){

  var mydata = $(document).data('my-data-attribute');

});

will the code above slowing the load time? 上面的代码会减慢加载时间吗?

or 要么

$('[data-suffix-attribute="some_value"]').each(function(){
   ......
});

or 要么

$('[data-suffix-attribute="delete"]').click(function(){
   // delete action happening here
});

will this bring trouble? 这会带来麻烦吗?

$(document).ready(function(){

  var mydata = $(document).data('my-data-attribute');

});

The code above will not work. 上面的代码不起作用。 If you want to read the HTML5 data attribute of an element using the jQuery .data() method firstly you need to select the relevant element using a jQuery selector and then you can use the method as is shown below: 如果要使用jQuery .data()方法读取元素的HTML5数据属性,则首先需要使用jQuery选择器选择相关元素,然后可以使用如下所示的方法:

var mydata = $('.example').data('suffix');

This will read the value of the data-suffix attribute of an element with a class of "example". 这将读取具有“示例”类的元素的数据后缀属性的值。

The other important thing to note when using the .data() method is that you have to omit the data- prefix from the selector to read the value stored in that attribute. 使用.data()方法时要注意的另一重要事项是,您必须从选择器中省略数据前缀,才能读取存储在该属性中的值。

The way you have selected the attribute before the .each() method will work: 在.each()方法生效之前选择属性的方式:

$('[data-suffix-attribute="some_value"]');

However, it would be better if you can narrow it down to a specific element like: 但是,最好将其范围缩小到特定元素,例如:

$('div[data-suffix-attribute="some_value"]');

This is because the first selector will go through every node in the document which will take more time whereas the second will only go through the div tags in the document. 这是因为第一个选择器将遍历文档中的每个节点,这将花费更多的时间,而第二个选择器将仅遍历文档中的div标签。

The attribute selector is supported by the native query selectors so it is fine. 属性选择器受本机查询选择器支持,因此很好。 As far as future is concerned I don't think in near future it will be a problem. 就未来而言,我认为在不久的将来这不会成为问题。

But it will be better if you can use a element selector attached to the attribute selector like $('div[data-suffix-attribute="delete"]') 但是如果可以使用附加到属性选择器上的元素选择器(例如$('div[data-suffix-attribute="delete"]')

If you are very worried about performance it will be a better choice to add a class attribute to the desired elements and then use class selector 如果您非常担心性能,则最好将类属性添加到所需的元素,然后使用类选择器

It would be better to use id in selector which is fast obviously, If you have multiple data attributes then it is better to use $('[data-suffix-attribute="delete"]').click(); 最好在selector中使用id最好fast ,如果selector中有multiple data attributes ,则最好使用$('[data-suffix-attribute="delete"]').click(); . Instead of this you can use the parent selector for your data-attribute elements like, 您可以使用parent selector代替您的data-attribute elements例如,

$('#parentId').on('click','[data-suffix-attribute="delete"]',function(){
   // delete action happening here
});

#parentId contains all data attribute elements #parentId包含所有data attribute elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM