試圖從catListView.render()中的model.data調用數組,它完美地顯示瞭如何使數組項可點擊(即單擊cat0或單擊cat2)。獲取單擊數組項目
$(function() {
var model = {
data: ["cat0", "cat1", "cat2", "cat3"],
}
var oct = {
init: function() {
catList.init();
},
getCat: function() {
return model.data;
},
};
var catListView = {
init: function() {
this.$catList = $("#cat-list");
catList.render();
},
render: function() {
var catList = this.$catList.html('');
var cats = oct.getCat();
for (var i = 0; i < cats.length; i++) {
var cat = cats[i];
var li = "<li>" + cat + "</li>";
addEventListener(li, "click", function(){
console.log(this.li.text());
});
catList.append(li);
}
}
};
oct.init();
}());
嘗試li.addEventListener( '點擊',函數(){ 的console.log( this.li.text()); }); –
已經試過這個,得到這個錯誤「Uncaught TypeError:li.addEventListener不是函數」 – faisal
var li =「