哪种jQuery方法更好:" .attr(' data-thing');"或& quot..data('事物');& quot; [英] Which jQuery method is better: ".attr('data-thing');" or ".data('thing');"

查看:54
本文介绍了哪种jQuery方法更好:" .attr(' data-thing');"或& quot..data('事物');& quot;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更好?

$('div').attr('data-stuff');

$('div').data('stuff');

该线程,我很想知道哪个更好用(如果有的话).

After having a discussion in the comments on this thread, I'm interested to know which is better to use, if any.

推荐答案

考虑到所有人所说的话,这里是答案":

Taking what all people have said into consideration, here is "the answer":

.data 更好,因为:

  1. 它的键入要短一些.
  2. 它可以保护您避免将来可能对数据结构进行更改(尽管不太可能).

.attr 更好,因为:

  1. 加载速度快了将近3倍.

我想我自己会继续使用 .attr ,但谢谢大家提供的信息:)

I think I will continue using .attr myself, but thank you all for this information :)

这篇关于哪种jQuery方法更好:" .attr(' data-thing');"或& quot..data('事物');& quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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