addEventListener to an article element in Javascript

前端 未结 2 2062
萌比男神i
萌比男神i 2020-12-04 04:21

I am trying to addEventListener to all article elements so when they are clicked it turns them into a draggable element.

I\'m not using jQuery on this task.

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 04:28

    document.getElementsByTagName returns a collection. So you need to iterate over it to add the event listeners. This collection is native DOM nodes collections and not a jquery collection hence you cannot use the addEventListener on the collection.

    var articles = document.getElementsByTagName("ARTICLE");
    var eventListener = function(){console.log('clicked an articles')}
    
    for(var i=0; i

提交回复
热议问题