JavaScript 回調函數

<!DOCTYPE>
<html>
 <head>
  <title> new document </title>
 </head>
 <body>
    <div id="div1">aaa </div>
    <div id="div2">bbb </div>
    <div id="div3">ccc </div>
 </body>
 <script type="text/javascript">
 
   function clickEvent(id,callback){
        var boxDom = document.getElementById(id);
        boxDom.onclick=function(){
            if(callback){javascript

           //經過參數將當前的dom元素返回出去
               callback(this);
            };
        };
   };

   clickEvent("div1",function(dom){
       dom.style.color = "red";
   });
   
   clickEvent("div2",function(dom){
       dom.style.fontSize = "60px";
   });
   
   clickEvent("div3",function(dom){
       dom.style.color = "red";
   });
 </script>
</html>

html

相關文章
相關標籤/搜索