AddEventListener to not exists object with only Javascript

后端 未结 2 1220
鱼传尺愫
鱼传尺愫 2021-01-07 04:57

I search for the whole stackoverflow but I didn\'t get any good result against this issues.Correct me if i\'m wrong.

I want to addEventListener to object that exists

2条回答
  •  失恋的感觉
    2021-01-07 05:29

    Well, first off you don't need the second document.getElementsByClssName, secondly IE has a special function.

    var buttons = document.getElementsByClassName("my-button");
    for(i=0;i

    EDIT:

    You could also use buttons array as something you create on the fly, ie :

    var button1 = document.createElement( 'button' );
    var button2 = document.createElement( 'button' );
    var buttons = [ button1, button2 ]
    
    // Rest of code above
    

提交回复
热议问题