首页 > React如何获取真实的Dom节点的属性值?

React如何获取真实的Dom节点的属性值?

比如设置一个
<li ref="myLi">test</li>
如何能使用this.refs.myLi得到test。
我试过了this.refs.myLi.value,this.refs.myLi.text都不可以?


这是文本节点,jquery写多了,和我一样。总是弄混了。


this.refs.myLi.innerText

The other big change we’re making in this release is exposing refs to
DOM components as the DOM node itself. That means: we looked at what
you can do with a ref to a React DOM component and realized that the
only useful thing you can do with it is call
this.refs.giraffe.getDOMNode() to get the underlying DOM node.
Starting with this release, this.refs.giraffe is the actual DOM node.
Note that refs to custom (user-defined) components work exactly as
before; only the built-in DOM components are affected by this change.

http://facebook.github.io/react/blog/2015/10/07/react-v0.14.html#dom-node-refs


使用ref属性获取Dom元素后,再使用原生javascript获取内容

ref具体使用请看文档 https://facebook.github.io/react/docs/more-about-refs.html


这个不叫元素的属性,这是dom元素下的textnode,应该用innerText去获取,另外用.text获取是不是因为jQuery $.text()写习惯了。

【热门文章】
【热门文章】