搜索
javascript - React如何获取真实的Dom节点的属性值?
PHP中文网
PHP中文网 2017-04-10 16:25:42
[JavaScript讨论组]

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

PHP中文网
PHP中文网

认证0级讲师

全部回复(4)
PHP中文网

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

PHP中文网
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

PHPz

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板