javascript - 找不到在运行时更改reactjs元素位置的方法

标签 javascript reactjs

我写了一些react.component,并将位置定义为固定。
我需要在运行时移动这个元素的位置并且

 render(){
    var t = <div className="myElement" />;
    t.top = '${500}px';
    t.left = '${900}px';

     return t;     // the element position need to be now 500, 900 
 }

最佳答案

看起来你只需要传递一些内联CSS规则:

render(){
  const s = {top: '500px', left: '900px'};
  return (
    <div className="myElement" style={s} />
  );
}

或者,更紧凑的版本:

render(){
  return (
    <div className="myElement" style={{top: '500px', left: '900px'}} />
  );
}

如果缺少单位,React 会自动px。所以你也可以这样做:{top: 500, left: 900}

<小时/>

来自 docs :

The style attribute accepts a JavaScript object with camelCased properties rather than a CSS string. This is consistent with the DOM style JavaScript property, is more efficient, and prevents XSS security holes.

关于javascript - 找不到在运行时更改reactjs元素位置的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50348795/

相关文章:

reactjs - React Native 初始路由名称

javascript - 从异步函数填充 React prop 对象

php - 如何将 php 函数放入 javascript onclick 函数中?

javascript - es6 中 const 的不变性

javascript - react 奖励五彩纸屑后的触发方法

javascript - 无法在 React 中将数据发布到 mlab

javascript - react native 元素复选框

javascript - Jquery SlickGrid - 为分组行添加区别

javascript - 在 Google 文档中创建彩虹文本

javascript - jQuery 宽度值错误