回调函数

回调函数基本的javascript代码:

function findNodes(func){

  //创建一个空的数组

  var nodes = [];

  //得到要用的东西

  var htmlNodes = document.getElementsByClassName('findLi');

  //把对象转换成数组

  nodes = Array.prototype.slice.call(htmlNodes);

  //判断是否为函数,是,添加进nodes数组中去

  if(typeof func === "function"){

    nodes.forEach(function(ele)){

      func(ele);  

      })

  }

  return nodes;

}

运用回调函数:

function hide(node){

  node.style.display  = "none";

}

function setBorder(node){

  node.style.border = "2px solid red";

}

findNodes(setBorder);

原文地址:https://www.cnblogs.com/xiaoxiaozhao/p/4119750.html