HTML5 data-* attribute type casting strings and numbers

后端 未结 6 648
生来不讨喜
生来不讨喜 2021-01-07 16: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 func

6条回答
  •  轮回少年
    2021-01-07 17:06

    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).

提交回复
热议问题