border-left-width返回jQuery中的NaN [英] Border-left-width returns NaN in jQuery

查看:200
本文介绍了border-left-width返回jQuery中的NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我忙于编写一小段代码,应该允许跨浏览器使用边框盒子模型。到目前为止基本功能工作正常,但我不能得到工作的边际。它应该适应可用的空间,但控制台返回一个NaN,我不知道它来自哪里。

I'm busy writing a small snippet of code that should allow cross-browser usage of the border-box box model. So far the basic functionality works fine, but I can't get the margin to work. It should adapt to the space that's available, but console returns a NaN and I have no idea where it comes from.

这里是一个小提琴。

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

The console.log doesn't even log anything and I don't know why. Any help is greatly appreciated.

推荐答案

在你的小提琴的很多部分你有这种模式:

In many parts of your fiddle you have this pattern:

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

您正试图清除 px / strong>执行一个乘法,导致语法错误。它应该是

You're trying to erase the px after executing a multiplication which results in a syntax error. It should be either

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

通过自动类型转换或

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

这会将字符串解析为整数,删除尾部 px

Which parses the string as an integer, removing the trailing px:


如果 parseInt 遇到一个不是
指定基数中的数字的字符,它忽略它和所有后续字符和
返回到该点解析的整数值。 parseInt
数字截断为整数值。允许使用前导和尾随空格。

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.

小提琴

parseInt 参考

这篇关于border-left-width返回jQuery中的NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆