javascript addEventListener在循环中的两个不同变量上 - >第一个听众不起作用

时间:2012-09-17 00:01:44

标签: javascript javascript-events addeventlistener

只能正确触发buttonEndNumber eventlistener。 buttonStartNumber没有做任何事情。

function appendNumberButtons()
    {
        var xpathExpression = 'id("delform")/div/div/div/div/div/div'
        var xpathResult = document.evaluate(xpathExpression, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)

        var buttonStartNumber
        var buttonEndNumber

        for ( var i=0 ; i < xpathResult.snapshotLength; i++ )
        {
            buttonStartNumber = document.createElement("input")
            buttonStartNumber.setAttribute('type', 'button')
            buttonStartNumber.setAttribute('value',"S"+i)
            //buttonStartNumber.addEventListener('click', function(i){ return function(I){alert(i)} }(i), false);
            listnerHelper(buttonStartNumber, i);


            buttonEndNumber = document.createElement("input")
            buttonEndNumber.setAttribute('type', 'button')
            buttonEndNumber.setAttribute('value',"E"+i)
            //buttonEndNumber.addEventListener('click', function(i){ return function(I){alert(i)} }(i), false);
            listnerHelper(buttonEndNumber, i);

            xpathResult.snapshotItem(i).innerHTML += " -> "
            xpathResult.snapshotItem(i).appendChild(buttonStartNumber)
            xpathResult.snapshotItem(i).innerHTML += " - "
            xpathResult.snapshotItem(i).appendChild(buttonEndNumber)
        }
    }

我尝试过的事情:

额外的函数listenerHelper(object,i) - &gt; NOP buttonStartNumber,buttonEndNumber作为带有和不带listenerHelper(object,i)的数组 - &gt; NOP

我的错误在哪里?

0 个答案:

没有答案