javascript ReactJs:是否可以按类名将 DOM 渲染为文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32556374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReactJs: Is it possible to render DOM to document by class name?
提问by Waleed Ahmad
I'm learning ReactJs and really like it. I wanted to ask if we can render virtual DOM to document by class name.
我正在学习 ReactJs 并且非常喜欢它。我想问一下我们是否可以按类名将虚拟 DOM 渲染到文档。
React.render(
<CommentBox url="data/comments.json" pollInterval={2000} />,
document.getElementById('class')
);
I tried to pass
我试图通过
getElementByClassName('class')
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?
作为 react 的 render 方法的第二个参数,它不起作用。React 是否只将 DOM 渲染到只有 ID 的节点,还是有一种解决方法也可以将节点与类一起使用?
回答by xcatliu
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.
可能你应该使用getElementsByClassName
而不是getElementByClassName
. 并且不要忘记getElementsByClassName
返回一个类似数组的 obj HTMLCollection,所以选择第一个元素是必要的。
React.render(
<CommentBox url="data/comments.json" pollInterval={2000} />,
document.getElementsByClassName('className')[0]
);
For more infomation, check out the docs.
有关更多信息,请查看文档。