小编Phi*_*ppe的帖子

如何在JS数组的所有元素之间插入一个新元素?

我有一个阵列[a, b, c].我希望能够给此阵列的像的各元件之间的插入值:[0, a, 0, b, 0, c, 0].

我想这会是这样的,但我不能让它奏效.

for (let i = 0; i < array.length; i++) {
    newArray = [
        ...array.splice(0, i),
        0,
        ...array.splice(i, array.length),
    ];
}
Run Code Online (Sandbox Code Playgroud)

感谢你们对我的帮助!

javascript arrays

24
推荐指数
4
解决办法
1万
查看次数

ReactJs 防止输入类型编号中的 e 和点

我想防止e.输入<input type="number"/>. 没有 jQuery 或使用step属性。我试过,pattern="[0-9]"但它不起作用。

编辑:焦点键盘应该是数字键盘。

html javascript input reactjs

11
推荐指数
3
解决办法
2万
查看次数

反应 onclick 数据值返回 null

我正在尝试将我的状态对齐更改为:左对齐、居中对齐、右对齐或对齐,具体取决于<Icon />我单击的对齐方式。但是,e.target.getAttribute('data-value')正在返回null

当我更改<Icon icon="align-left" />by 时left,它正在工作。e.target.getAttribute('data-value')正在返回数据值。

所以,我怎样才能改变我的状态,以左,右中心或我的点击自圆其说<Icon />

class TextStyleParams extends React.Component {
  constructor(props) {
    super(props);
    this.onClickAlignment = this.onClickAlignment.bind(this);

    this.state = {
      textStyle: {
        alignment: 'left',
        ...,
      },
    };
  }

  onClickAlignment(e) {
    const { textStyle } = this.state;
    const newTextStyle = {
      ...textStyle,
      alignment: e.target.getAttribute('data-value'),
    };
    this.setState({ textStyle: newTextStyle });
    this.props.updateTextStyle(newTextStyle);
  }
    
  render() {
    const { textStyle } = this.state;
        
    return (
      <div>
        <span role="button" onClick={this.onClickAlignment} …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

6
推荐指数
1
解决办法
1489
查看次数

标签 统计

javascript ×3

reactjs ×2

arrays ×1

html ×1

input ×1