循环传值_闭包

<p id="btn1" itemid='1'>1</p>

    <p id="btn2" itemid="2">2</p>

    <p id="btn3" itemid="3">4</p>

    <p id="btn4" itemid="4">4</p>

    <button id="subbtn">提交</button>

    <script type="text/javascript">

    var btn = document.getElementsByTagName('p');

    var itemid = [];

     

    for(var i=0,len=btn.length;i<len;i++)

    {

        

        (function(index){

        var  _this = btn[index];

            _this.onclick = function(){

            _this.style.background = '#ccc';

            if(_this.className!=='checked')

            {

                

            }

            _this.className = 'checked';

            itemid.push(_this.getAttribute('itemid')); 

        }})(i)

    }

    subbtn.onclick = function(){

        alert(itemid.join(','));

    }

    </script>

 

你可能感兴趣的:(闭包)