开发者

How to add eventListener each separate li into each separate function?

I am using the function, to add each li into each function, using addEven开发者_运维技巧tListener. According to me, on click on each li, should call each separate function. how can i add the li's separately each functions? any one can help me..?

window.onload = function(){

            var myLi = document.getElementById('ul').getElementsByTagName('li');

            for(i=0;i<=myLi.length;i++){
                myLi[i].addEventListener('click','call'+[i],false);
            }

            function call(){
                alert('function one called');
            }

            function call2(){
                alert('function two called');
            }

            function call3(){
                alert('function three called');
            }

        }


Use an array to store your functions,

var calls = [];

calls[0] = function() { alert("1 called"); }
calls[1] = function() { alert("2 called"); }
..

Then add the event listeners as,

myLi[i].addEventListener('click', calls[i], false);

See an example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜