Vanilla JavaScript version of jQuery .click

后端 未结 7 661
孤城傲影
孤城傲影 2020-12-07 22:24

So maybe I\'m just not looking in the right places but I can\'t find a good explanation of how to do the equivalent of jQuery\'s

$(\'a\').click(function(){
          


        
7条回答
  •  心在旅途
    2020-12-07 22:43

    This will assign an onclick function to every a element.

    var links = document.getElementsByTagName("a");
    var linkClick = function() {
      //code here
    };
    
    for(var i = 0; i < links.length; i++){
      links[i].onclick = linkClick;
    }
    

    You can see it in action here.

提交回复
热议问题