带有 + 的 jQuery 数据值未正确检索

标签 jquery html

如果我有一个元素

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/

相关文章:

javascript - jquery 克隆日期选择器不工作

jquery - 使用 jQuery 定位 (this) 未按预期工作

javascript - 为什么输入元素的值没有改变?

c# - 在重定向时显示动画 gif 文件直到 asp 页面加载?

javascript - 重新打开加载的 jQuery div 对话框

javascript - Jquery slider 无法正常工作?

javascript - 在 h1 标签上移动时闪烁

javascript - 使用 jquery 或 javascript 在悬停图像时更改 href 属性

javascript - 如何更改 Bootstrap slider 的大小?

javascript - JQUERY hover() 函数不适用于其子项,使用引导框架