首页 > 解决方案 > 在javascript中获取子字符串并转换为整数

问题描述

const [top, bottom, left, right] = [txtStyle.getPropertyValue('top'),
          txtStyle.getPropertyValue('bottom'),
          txtStyle.getPropertyValue('left'),
          txtStyle.getPropertyValue('right')];

值被分配为“24px”等。但我想将其分配为 24(int)。最好的方法是什么?

标签: javascriptangulartypescript

解决方案


您可以使用parseInt

const [top, bottom, left, right] = [parseInt(txtStyle.getPropertyValue('top')),
  parseInt(txtStyle.getPropertyValue('bottom')),
  parseInt(txtStyle.getPropertyValue('left')),
  parseInt(txtStyle.getPropertyValue('right'))
];


推荐阅读