jquery - jQuery 中的左边框宽度返回 NaN

标签 jquery css nan border-box

我正忙着编写一小段代码,该代码应该允许跨浏览器使用边框框模型。到目前为止,基本功能运行良好,但我无法让余量发挥作用。它应该适应可用的空间,但控制台返回 NaN,我不知道它来自哪里。

Here是一把 fiddle 。

console.log 甚至没有记录任何内容,我不知道为什么。非常感谢任何帮助。

最佳答案

在你的 fiddle 的许多部分都有这种模式:

($(this).css("border-left-width") * 2).replace("px", "")

您试图在执行乘法之后删除px,这会导致语法错误。应该是

($(this).css("border-left-width").replace("px", "") * 2)

通过自动类型转换工作或

(parseInt($(this).css("border-left-width"), 10) * 2)

它将字符串解析为整数,删除尾随的 px:

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

Fiddle

parseInt reference

关于jquery - jQuery 中的左边框宽度返回 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12232056/

相关文章:

javascript - 如何避免特定类应用css

html - 尝试将我的标签放在输入类型文本上方

css - 使用 sed 解压缩样式表

Python pandas fillna 只有一行具有特定值

python - 如何在Python中替换字典中的NaN

php - Github 发布 jquery 插件

jquery - 扩展的 div-content 压低其他 div

html - 在 HTML 和 CSS 代码中定位元素

python - Theano 随机梯度下降 NaN 输出

当模式匹配字符串时,Javascript/jQuery 比 $.inArray 更快的替代方案