getElementByClassName() - is not a function [closed]

我的未来我决定 提交于 2020-01-30 13:28:06

问题


I expected the code below to change Random1 and Random2 to Random but its not doing anything. When I checked developers tool in chrome, it showed "document.getElementByClassName is not a function". Any help will be appreciated.

My HTML:

<h3 class="Rand"><i>Random1 </i></h3>
<h3 class="Rand">Random2</h3>

My JavaScript:

var elems = document.getElementByClassName('Rand');     
for(var i = 0; i <= elems.length; i++)
{
elems[i].innerHTML = "Random";
}

回答1:


You should use getElementsByClassName instead using getElementByClassName. It seems you misspelled.




回答2:


It seems to be an undefined function you used to call

The class based selection returns an array of objects which is because of multiple elemnts have same class so the function should be getElementsByClassName instead you used getElementByClassName If it is for Id selector getElementById is enough because there is only instance might be there in the DOM



来源:https://stackoverflow.com/questions/44448891/getelementbyclassname-is-not-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!