ReactJs:是否可以按类名将DOM渲染为文档? [英] ReactJs: Is it possible to render DOM to document by class name?
问题描述
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屋!