Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中获得边框宽度的更好方法?_Javascript_Css_Getcomputedstyle - Fatal编程技术网

在javascript中获得边框宽度的更好方法?

在javascript中获得边框宽度的更好方法?,javascript,css,getcomputedstyle,Javascript,Css,Getcomputedstyle,我用这个: var playGard =document.getElementsByClassName("playGard")[0]; var borderW= getComputedStyle(playGard,null).getPropertyValue('border-left-width').substr(0,2); 我得到值“10” 在javascript中有更好的缩短方法吗 (以数字形式获取值)显然,substr(0,2)不是一个好主意,因为如果宽度小于10或大于100,它将不起

我用这个:

var playGard =document.getElementsByClassName("playGard")[0];

var borderW= getComputedStyle(playGard,null).getPropertyValue('border-left-width').substr(0,2);
我得到值“10”

在javascript中有更好的缩短方法吗

(以数字形式获取值)

显然,
substr(0,2)
不是一个好主意,因为如果宽度小于10或大于100,它将不起作用

相反,只需删除“px”字符串(如果存在;如果值为0,则可能不存在):

前导的
+
是将其转换为数字

但是为什么呢? 更有趣的问题是,为什么要尝试获取边界宽度。你打算用它做什么?使用
getComputedStyle
有点反模式。它通常表示您试图在CSS中维护应用程序状态,这绝不是一个好主意。

显然,
substr(0,2)
不是一个好主意,因为如果宽度小于10或大于100,它将不起作用

相反,只需删除“px”字符串(如果存在;如果值为0,则可能不存在):

前导的
+
是将其转换为数字

但是为什么呢?
更有趣的问题是,为什么要尝试获取边界宽度。你打算用它做什么?使用
getComputedStyle
有点反模式。它通常表示您试图在CSS中维护应用程序状态,这绝不是一个好主意。

似乎是正确的,这是获得计算样式的方法您的问题本身就有答案!document.getElementById(“ppp”).style.border.可能存在重复。。这会给你border@SajadKaruthedath是的,只使用javascript。似乎是对的,这是获得计算样式的方法。你的问题本身就有你的答案!document.getElementById(“ppp”).style.border.可能存在重复。。这会给你border@SajadKaruthedath是的,仅使用javascript。它用于获取无边框的宽度/高度。我与一个圆圈玩一个小游戏,如果触摸边框,圆圈将变为红色。它用于获取无边框的宽度/高度。我与一个圆圈玩一个小游戏,如果触摸边框,圆圈将变为红色。
+val.replace('px', '')