如果我有一个元素
data-something="+5"
然后尝试:
alert($(el).data('something')); //gives 5, not +5...
如何取回原始值?
最佳答案
引用 jquery 手册,关于使用 .data() 访问数据属性的提取 -
Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.
看起来您的 +5 被解释为正整数 5。
关于带有 + 的 jQuery 数据值未正确检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060105/