<tfoot id='sX7OD'></tfoot>

      <i id='sX7OD'><tr id='sX7OD'><dt id='sX7OD'><q id='sX7OD'><span id='sX7OD'><b id='sX7OD'><form id='sX7OD'><ins id='sX7OD'></ins><ul id='sX7OD'></ul><sub id='sX7OD'></sub></form><legend id='sX7OD'></legend><bdo id='sX7OD'><pre id='sX7OD'><center id='sX7OD'></center></pre></bdo></b><th id='sX7OD'></th></span></q></dt></tr></i><div id='sX7OD'><tfoot id='sX7OD'></tfoot><dl id='sX7OD'><fieldset id='sX7OD'></fieldset></dl></div>

      1. <legend id='sX7OD'><style id='sX7OD'><dir id='sX7OD'><q id='sX7OD'></q></dir></style></legend>
          <bdo id='sX7OD'></bdo><ul id='sX7OD'></ul>

        <small id='sX7OD'></small><noframes id='sX7OD'>

      2. 如何从 React 访问样式?

        时间:2023-09-08

        • <i id='fM6Qv'><tr id='fM6Qv'><dt id='fM6Qv'><q id='fM6Qv'><span id='fM6Qv'><b id='fM6Qv'><form id='fM6Qv'><ins id='fM6Qv'></ins><ul id='fM6Qv'></ul><sub id='fM6Qv'></sub></form><legend id='fM6Qv'></legend><bdo id='fM6Qv'><pre id='fM6Qv'><center id='fM6Qv'></center></pre></bdo></b><th id='fM6Qv'></th></span></q></dt></tr></i><div id='fM6Qv'><tfoot id='fM6Qv'></tfoot><dl id='fM6Qv'><fieldset id='fM6Qv'></fieldset></dl></div>
            <bdo id='fM6Qv'></bdo><ul id='fM6Qv'></ul>

          • <legend id='fM6Qv'><style id='fM6Qv'><dir id='fM6Qv'><q id='fM6Qv'></q></dir></style></legend>
          • <tfoot id='fM6Qv'></tfoot>
              <tbody id='fM6Qv'></tbody>

              <small id='fM6Qv'></small><noframes id='fM6Qv'>

                • 本文介绍了如何从 React 访问样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我试图在 React 中访问 div 的宽度和高度样式,但我遇到了一个问题.这是我到目前为止得到的:

                  componentDidMount() {console.log(this.refs.container.style);}使成为()  {返回 (<div ref={"container"} className={"container"}></div>//设置参考);}

                  这可行,但我得到的输出是一个 CSSStyleDeclaration 对象,在 all 属性中,我可以为该对象设置所有 CSS 选择器,但它们都没有设置.它们都设置为空字符串.

                  这是 CSSStyleDecleration 的输出:http://pastebin.com/wXRPxz5p

                  任何有关查看实际样式(事件继承的样式)的帮助将不胜感激!

                  谢谢!

                  解决方案

                  For React v <= 15

                  console.log(ReactDOM.findDOMNode(this.refs.container).style);//反应 v >0.14console.log(React.findDOMNode(this.refs.container).style);//React v <= 0.13.3

                  <块引用>

                  用于获取具体的样式值

                  console.log(window.getComputedStyle(ReactDOM.findDOMNode(this.refs.container)).getPropertyValue("border-radius"));//border-radius 可以替换为任何其他样式属性;

                  <块引用>

                  对于反应 v>= 16

                  使用回调样式或使用 createRef() 分配 ref.

                  assignRef = element =>{this.container = 元素;}getStyle = () =>{常量样式 = this.container.style;控制台.log(样式);//用于获取计算样式const computed = window.getComputedStyle(this.container).getPropertyValue("border-radius"));//border-radius 可以替换为任何其他样式属性;控制台.log(计算);}

                  I am trying to access the width and height styles of a div in React but I have been running into one problem. This is what I got so far:

                  componentDidMount()  {
                    console.log(this.refs.container.style);     
                  }
                  
                  
                  render()  {
                     return (
                        <div ref={"container"} className={"container"}></div>  //set reff
                     );
                  }
                  

                  This works but the output that I get is a CSSStyleDeclaration object and in the all property I can all the CSS selectors for that object but they none of them are set. They are all set to an empty string.

                  This is the output of the CSSStyleDecleration is: http://pastebin.com/wXRPxz5p

                  Any help on getting to see the actual styles (event inherrited ones) would be greatly appreciated!

                  Thanks!

                  解决方案

                  For React v <= 15

                  console.log( ReactDOM.findDOMNode(this.refs.container).style); //React v > 0.14
                  
                  console.log( React.findDOMNode(this.refs.container).style);//React v <= 0.13.3
                  

                  EDIT:

                  For getting the specific style value

                  console.log(window.getComputedStyle(ReactDOM.findDOMNode(this.refs.container)).getPropertyValue("border-radius"));// border-radius can be replaced with any other style attributes;
                  

                  For React v>= 16

                  assign ref using callback style or by using createRef().

                  assignRef = element => {
                    this.container = element;
                  }
                  getStyle = () => {
                  
                    const styles = this.container.style;
                    console.log(styles);
                    // for getting computed styles
                    const computed = window.getComputedStyle(this.container).getPropertyValue("border-radius"));// border-radius can be replaced with any other style attributes;
                    console.log(computed);
                  }
                  

                  这篇关于如何从 React 访问样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:确定 javascript 对象上的所有属性是 null 还是空字 下一篇:是否可以使用 Angular2 有条件地显示元素属性?

                  相关文章

                  最新文章

                • <small id='QlyXc'></small><noframes id='QlyXc'>

                  1. <legend id='QlyXc'><style id='QlyXc'><dir id='QlyXc'><q id='QlyXc'></q></dir></style></legend>
                  2. <i id='QlyXc'><tr id='QlyXc'><dt id='QlyXc'><q id='QlyXc'><span id='QlyXc'><b id='QlyXc'><form id='QlyXc'><ins id='QlyXc'></ins><ul id='QlyXc'></ul><sub id='QlyXc'></sub></form><legend id='QlyXc'></legend><bdo id='QlyXc'><pre id='QlyXc'><center id='QlyXc'></center></pre></bdo></b><th id='QlyXc'></th></span></q></dt></tr></i><div id='QlyXc'><tfoot id='QlyXc'></tfoot><dl id='QlyXc'><fieldset id='QlyXc'></fieldset></dl></div>
                  3. <tfoot id='QlyXc'></tfoot>

                      <bdo id='QlyXc'></bdo><ul id='QlyXc'></ul>