如何从事件侦听器传递数据集

时间:2016-12-12 09:25:56

标签: javascript cordova

它没有通过任何事情 如果我在for循环上发送索引,它会发送所有最后一个单元格索引

我希望区分元素具有相同的类 我想要替代使用' unsafe-inline'并通过html发送参数

$(document).on('deviceready', function () {
    console.log("Device is ready!");
    //myApp.alert("aa","aa");

    var cell = document.querySelectorAll('.item-title');

    for (var i = 0; i < cell.length; i++) {
        cell[i].addEventListener("click", function() {
            aaa(cell[i].dataset.index);
        }, true);

        //myApp.alert("aa"+, "aa");
        // x[i]=i;
    }
});

function aaa(id) {
    myApp.alert(id);
}

1 个答案:

答案 0 :(得分:0)

$(document).on('deviceready', function () {
    console.log("Device is ready!");
    //myApp.alert("aa","aa");

    var cell = document.querySelectorAll('.item-title');

    for (var i = 0; i < cell.length; i++) {
        cell[i].addEventListener("click", function( event ) {
            const id = event.target.dataset.id;
            aaa(id);
        }, true);

        //myApp.alert("aa"+, "aa");
        // x[i]=i;
    }
});

function aaa(id) {
    myApp.alert(id);
}