getElementsByClassName Help (Uncaught reference error when assigning to variable)

前端 未结 3 1294
说谎
说谎 2021-01-22 06:16

Having some trouble selecting class names with plain \'ol vanilla javascript:

var email = document.getElementsByClassName(\'email\');
var phone = document.getEle         


        
3条回答
  •  花落未央
    2021-01-22 06:55

    Hint:

    document.getElementsByClassName -- Notice that is plural. It returns an nodeList or a null.

    First step of debugging should have been a console.log(phone) and see what value it did have.

    Without seeing your markup, I have to take some guesses but try:

    var email = document.getElementsByClassName('email')[0];
    var phone = document.getElementsByClassName('phone')[0];
    

    Edit: Returns a nodeList which is array-like, but not actually an array.

提交回复
热议问题