ReactJs:是否可以按类名将DOM渲染为文档? [英] ReactJs: Is it possible to render DOM to document by class name?

查看:82
本文介绍了ReactJs:是否可以按类名将DOM渲染为文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习ReactJs,真的很喜欢它。我想问我们是否可以通过类名提供虚拟DOM文档。

  React.render(
< commentBox url =data / comments.jsonpollInterval = {2000} />
document.getElementById('class')
);

我试图通过


getElementByClassName('class')


作为反应渲染方法的第二个参数,它不起作用。 React只会将DOM渲染到仅具有ID的节点,或者是否还有使用节点的解决方法?

解决方案

使用错误的方法。



可能您应该使用 getElementsByClassName 而不是 getElementByClassName 。并且不要忘记 getElementsByClassName 返回一个类似数组的obj HTMLCollection ,所以选择第一个元素是必要的。

  React.render (
< CommentBox url =data / comments.jsonpollInterval = {2000} />
document.getElementsByClassName('className')[0]
);

有关更多信息,请查看 docs


I'm learning ReactJs and really like it. I wanted to ask if we can render virtual DOM to document by class name.

React.render(
    <CommentBox url="data/comments.json" pollInterval={2000} />,
    document.getElementById('class')
);

I tried to pass

getElementByClassName('class')

as second argument to react's render method and it doesn't work. Does React only render DOM to nodes with ID's only or is there's a workaround to use nodes with classes too?

解决方案

It seems you have used a wrong method.

Probably you should use getElementsByClassName instead of getElementByClassName. And DO NOT forget getElementsByClassName returns an array-like obj HTMLCollection, so picking the first element is necessary.

React.render(
  <CommentBox url="data/comments.json" pollInterval={2000} />,
  document.getElementsByClassName('className')[0]
);

For more infomation, check out the docs.

这篇关于ReactJs:是否可以按类名将DOM渲染为文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆