动态创建dom节点,并用回调函数控制样式,实现代码复用

版权声明:内容多为自言自语,请自行判断有无价值。 https://blog.csdn.net/weixin_41702247/article/details/83054849
var appendDiv=function(count,cb){
    for(let i=0;i<count;i++){
        let div=document.createElement('div');    //创建
        div.innerHTML=`This is the ${i} div`;     //设置内容
        document.body.appendChild(div);           //插入页面
        if(typeof cb==='function'){               //判断第二参数是否为函数,是的话就把div作为参数传进去并执行
            cb(div);
        }
    }
}

appendDiv(3,node=>node.style.color='#666');

猜你喜欢

转载自blog.csdn.net/weixin_41702247/article/details/83054849