简体   繁体   English

使用jQuery data()vs native javascript getAttribute vs input hidden

[英]Using jQuery data() vs native javascript getAttribute vs input hidden

I need to access general information from my site using javascript. 我需要使用javascript从我的网站访问一般信息。 So far, I have the following options: 到目前为止,我有以下选择:

  • Using an html element : <input type="hidden" value="MyValue"/> 使用html元素: <input type="hidden" value="MyValue"/>

  • Using a custom attribute in an existing html element : <div id="HtmlElement" myattr="MyValue"></div> and then access it using document.getElementById("HtmlElement").getAttribute("myattr") 在现有的html元素中使用自定义属性: <div id="HtmlElement" myattr="MyValue"></div>然后使用document.getElementById("HtmlElement").getAttribute("myattr")访问它document.getElementById("HtmlElement").getAttribute("myattr")

  • Using a data attribute in an existing html element: <div id="HtmlElement" data-myattr="MyValue"></div> and then access it using jQuery("#HtmlElement").data("myattr") 在现有的html元素中使用data属性: <div id="HtmlElement" data-myattr="MyValue"></div>然后使用jQuery("#HtmlElement").data("myattr")访问它jQuery("#HtmlElement").data("myattr")

I was wondering what are the benefits of using either option. 我想知道使用任一选项有什么好处。

I'm not a fan of using hidden inputs because I don't like the idea of having a loose html element that contains information. 我不喜欢使用隐藏的输入,因为我不喜欢使用包含信息的松散html元素。 But since I need it to display general information, not information related to an existing html element in the page, it doesn't seem so bad. 但是因为我需要它来显示一般信息,而不是与页面中现有html元素相关的信息,所以它看起来并不那么糟糕。

On the other side, I'm not a fan of abusing the use of an external library but in my case I'm allready loading jQuery in my site, so it's not as if i was loading an entire library just for this. 另一方面,我不是滥用外部库的粉丝,但在我的情况下,我已经在我的网站上加载了jQuery,所以这并不像我为此加载整个库。

And finally, even dough performance is allways an issue, in my case it's not gonna make much difference if it's the fastest solution. 最后,即使是面团性能也是一个问题,在我的情况下,如果这是最快的解决方案,它将不会产生太大的影响。

I would go with data attributes because that's what they are for and modern browsers have a native api for accessing them, while still allowing non-modern browsers to access them as custom attributes. 我会使用数据属性,因为它是它们的用途,现代浏览器有一个本机API用于访问它们,同时仍然允许非现代浏览器作为自定义属性访问它们。

given this html: 给出这个HTML:

<div data-foo="bar"></div>

// modern browser:
foo = document.getElementByID("myelementid").dataset.foo;

// older browser:
foo = document.getElementByID("myelementid").getAttribute("data-foo");

// jQuery (all browsers)
foo = $("#myelementid").data("foo");

Note if your data attribute is data-foo-bar , the key in dataset and .data will be fooBar 请注意,如果您的数据属性是data-foo-bar ,则dataset.data的键将为fooBar

As sdespont mentions, the data should be relevant to the element you are storing it on. 正如sdespont所提到的,数据应该与您存储它的元素相关。

Update: 更新:
It's also important to note that you can also get the value of a data attribute using the .attr method, however there is a pretty important difference between the two. 同样重要的是要注意,您也可以使用.attr方法获取数据属性的值,但两者之间存在非常重要的差异。 Getting a data attribute's value with .data will attempt to parse the value of the attribute into the correct javascript type , for example, if it can be converted to an int, it will be converted to an int. 使用.data获取数据属性的值将尝试将属性的值解析为正确的javascript 类型 ,例如,如果它可以转换为int,则它将转换为int。 If it can be converted into an object or an array, it will be converted to an object or an array. 如果它可以转换为对象或数组,它将被转换为对象或数组。 If you instead used .attr() , you can be sure that you will always have a string. 如果你改为使用.attr() ,你可以确定你总是有一个字符串。


Probably also worth mentioning that using .attr() should be prefered over .data() unless you specifically need the features given by .data() due to the fact that by using .data() , a data cache will be created for that element and it's data, which isn't needed unless you actually intend to use .data() multiple times or need the extra features provided by .data() 也许还值得一提的是,使用.attr()应在者优先.data()除非特别需要通过给出的特征.data()通过使用由于.data()数据高速缓存将针对创建元素及其数据,除非您实际打算多次使用.data()或需要.data()提供的额外功能,否则不需要

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

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