问题
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