document.getElementsByClassName()[removed] always returns “undefined”

后端 未结 1 1742
醉梦人生
醉梦人生 2020-11-28 10:28

I must have made a mistake somewhere so the document.getElementsByClassName().innerHTML is always returning undefined.

First i generate

相关标签:
1条回答
  • 2020-11-28 11:20

    document.getElementsByClassName() returns a nodeList, not an element!

    So it should be :

    document.getElementsByClassName('hidden')[0].innerHTML
    

    and as you probably have more .hidden elements, and only want the one inside the current .box (which would be this in the event handler)

    this.getElementsByClassName('hidden')[0].innerHTML
    

    but why not jQuery

    $(".box").click(function(){
            alert( $('.hidden', this).html() );
    });
    
    0 讨论(0)
提交回复
热议问题