简体   繁体   English

为什么jquery从数据html5属性中删除括号?

[英]Why does jquery remove brackets from data html5 attribute?

I am trying to get the content of a data attribute with jquery but returned data is not what I had set. 我正在尝试使用jquery获取数据属性的内容,但返回的数据不是我设置的。

With this simple example: 用这个简单的例子:

<div id="test" data-test="[1]"></div>

But $('#test').data('test') returns 1 instead of [1] 但是$('#test').data('test')返回1而不是[1]

No problem using pure javascript. 使用纯JavaScript没问题。

View it online: https://jsfiddle.net/jojhm2nd/ 在线查看: https//jsfiddle.net/jojhm2nd/

jQuery's data is not an attribute accessor function. jQuery的data不是属性访问器函数。 (This is a common mistake, easily made.) To just access the attribute, use attr . (这是一个容易犯的常见错误。)要访问属性,请使用attr

$("#test").attr("data-test");

data does read data-* attributes, but only to initialize jQuery's data cache for that element. data确实读取data-*属性,但仅用于初始化该元素的jQuery数据缓存。 (And it never writes attributes.) It does a whole series of things including changing names ( data-testing-one-two-three becomes testingOneTwoThree , for instance) and interpreting the values. (并且它从不属性。)它完成了一系列的工作,包括更改名称(例如, data-testing-one-two-three变成了testingOneTwoThree )并解释值。 In this case, it interprets the value as an array because it starts with [ . 在这种情况下,它将值解释为数组,因为它以[开头。 When you show that array with alert , it's coerced to a string, and when you coerce an array to a string, that does an Array#join . 当显示带有alert数组时,将其强制为字符串,将数组强制为字符串时,将执行Array#join If your attribute had been [1, 2] , for instance, you'd've seen 1,2 as the result. 例如,如果您的属性为[1, 2] ,则结果将为1,2

From the docs linked above: 从上面链接的文档中:

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). 会尝试将字符串转换为JavaScript值(包括布尔值,数字,对象,数组和null)。 A value is only converted to a number if doing so doesn't change the value's representation. 仅在不更改值表示形式的情况下,将值转换为数字。 For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. 例如,“ 1E02”和“ 100.000”等价于数字(数值100),但对其进行转换将改变其表示形式,因此它们保留为字符串。 The string value "100" is converted to the number 100. 字符串值“ 100”将转换为数字100。

When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; 当data属性是一个对象(以'{'开头)或数组(以'['开头)时,则使用jQuery.parseJSON来解析字符串; it must follow valid JSON syntax including quoted property names. 它必须遵循有效的JSON语法,包括带引号的属性名称。 If the value isn't parseable as a JavaScript value, it is left as a string. 如果该值不能作为JavaScript值解析,则将其保留为字符串。

jQuery does magic when you use the .data method. 当您使用.data方法时,jQuery会发挥神奇作用。

From the jQuery website: 从jQuery网站:

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). 会尝试将字符串转换为JavaScript值(包括布尔值,数字,对象,数组和null)。

You can use the .attr method and do: 您可以使用.attr方法并执行以下操作:

$('#test').attr('data-test');

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

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