开发者

jQuery.data parsing string like 10E6 as int (10000000)

Given an element like this <a data-test="10E6"> jQuery interprets the value as an integer in scientific notaion rather than a string as intended when I do elem.data('test')

Given that this is happening in a fairly large application I'm exploring my options for changing this behavior without having to switch to using elem.attr('data-test') everywhere that this may happen.

Since jQuery uses $.isNaN internally before trying to parse the value as a float I could override isNaN adding the regex ^[E\d]+$ like so:

$.isNaN = function( obj ) {
    return obj == null || !$.rdigit.test( obj ) || /E/.test( obj ) || isNaN( obj );
}

or override the much more complex $.data

Does an开发者_运维百科yone have a better plan?


According to the documentation of .data() itself, the jQuery developers tell you to use .attr() to retrieve the value without coercing it. I'd imagine overriding the internals that jQuery is using would be a more detrimental problem than simply switching to the appropriate method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜