0

如何将 getProjectssexy(1) 更改为我需要 getProjectssexy(0)、getProjectssexy(1) 等的每个元素的编号(取决于 tmpArray.length)

  $('allarray', data).each(function(){
            tmpArray = $('array', this).toArray();
                var table = document.getElementById("tleft");
                table.innerHTML = "";
                for(i =0; i<tmpArray.length; i++) {
                    table.innerHTML += "<div class='two'>" +
                    "<a href='#' onclick='getProjectssexy(1 - need to change i for every loop)'>" +
                    tmpArray[i].getAttribute("id") + "</a>" +"</div>" + "</br>";
                }
            });
4

2 回答 2

0

您必须将其存储在元素上的某个位置。您可以使用html5 data-*属性来存储它,并使用jQuery .data()来检索它。

$('allarray', data).each(function(){
        tmpArray = $('array', this).toArray();
            var table = document.getElementById("tleft");
            table.innerHTML = "";
            for(i =0; i<tmpArray.length; i++) {
                table.innerHTML += "<div class='two'>" +
                "<a href='#' data-i='" + i + "' onclick='getProjectssexy(this)'>" +
                tmpArray[i].getAttribute("id") + "</a>" +"</div>" + "</br>";
            }
        });

function getProjectssexy(elem) {
    var i = $(elem).data('i');
    ....
}
于 2013-09-15T14:52:12.723 回答
0

基本解决方案是

$('allarray', data).each(function(){
            tmpArray = $('array', this).toArray();
                var table = document.getElementById("tleft");
                table.innerHTML = "";
                for(i =0; i<tmpArray.length; i++) {
                    table.innerHTML += "<div class='two'>" +
                    "<a href='#' onclick='getProjectssexy("+i+")'>" +
                    tmpArray[i].getAttribute("id") + "</a>" +"</div>" + "</br>";
                }
            });
于 2013-09-15T15:25:42.697 回答