HTML5 data-* attribute type casting strings and numbers

时光总嘲笑我的痴心妄想 提交于 2019-11-30 21:25:18

问题


Why is the value of data-value="2.0" cast to a String and the value of data-value="2.5" cast to a Number? I can handle this fine within my function. I'm just trying to understand a little more about how Javascript handles Numbers and Strings. This kind of caught me off guard.

<a data-value="2.0">2.0</a>
<a data-value="2.5">2.5</a>
$("a").click(function() {
    alert(typeof $(this).data( "value"));   
});

[ Fiddle With It ]


回答1:


Those values are simply strings to vanilla javascript; it's not attempting any conversion on its own.

[...document.querySelectorAll("a")].forEach(a =>
    console.log("type: %s, value: %o", 
                typeof a.dataset.value, 
                a.dataset.value)
);
<a data-value="2.0">2.0</a>
<a data-value="2.5">2.5</a>

jQuery, on the other hand, tries to determine and convert to the appropriate type when accessing data attributes via data(). It's (arguably) an issue with their implementation. Their documentation (emphasis mine) actually addresses this:

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and 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. The string value "100" is converted to the number 100.

See also HTMLElement.dataset




回答2:


Looking through jQuery's source code, i have identified the reason.

It will parse it as a number, if, and only if, doing so does not change the string. Source: https://github.com/jquery/jquery/blob/master/src/data.js#L36

(+"2.0") + "" === "2" // !== the original string
(+"2.1") + "" === "2.1" // == the original string



回答3:


By default anything parsed from an attribute will be a string, you will need to convert it to a number if you need to use it as a number. The best way I've been able to handle this is by wrapping it with the Number function (native javascript)

var number = Number($(this).data("value"));
parseInt will return an integer (whole number), so if you want to keep your decimal values you'll need to use Number. Word of caution with parseInt, it is always better to specify the base if you want to parse an int, parseInt($(this).data("value"), 10);


回答4:


As tymeJV mentioned, this looks like an issue with how jquery handles autoconversion. If you use "2", it gives a number as well, so I'm guessing its just a weird edge case in how they handle things. I would encourage just using .attr('xxx') and parsing out your data to its known type.




回答5:


It appears to be treating that ".0" extension of the number like a string.

If all of your data values are going to be coming in in that format, just whip a parseFloat() on those suckers like this:

$("a").click(function() {
    alert(typeof parseFloat($(this).data( "value")));   
});

That way it won't matter if they are strings or numbers, they will always be treated like numbers(decimals intact).




回答6:


I think it's more a question about how jquery identifies numbers. If you use alert(typeof(this.getAttribute("data-value")));, it spits out that it's a string both times (which is expected). So far as I know, everything in an html attribute is considered a string, just different libraries may have default behavior that interprets them differently.

Also, a shorthand way to get a number would be something along the lines of...

var someNumber = +$(someElt).data("value");

the + will cause type coercion if the returned value is a string, or leave it alone if it's already a number.



来源:https://stackoverflow.com/questions/26126017/html5-data-attribute-type-casting-strings-and-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!