HTML5 data-* attribute type casting strings and numbers

后端 未结 6 609
生来不讨喜
生来不讨喜 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:04

    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
    

提交回复
热议问题