我創建了一個名爲「Ball」的對象。 文檔加載後,腳本將創建對象的實例12,並插入如此多的元素裏,以爲什麼alert函數不顯示每個的索引
<ul></ul>
我的目的是,當點擊球,它顯示球的指數。 爲〔實施例:單擊否3球,這表明3 但是當我點擊每個球,它總是顯示12
對不起,我不能上傳快照html文件,因爲我在這裏是新來的傢伙。
function Ball(idx, parent, libra) {
this.idx = idx;
this.parent = parent;
this.libra = libra;
this.init();
}
Ball.r = 30;
Ball.prototype = {
init: function() {
var _html = $("<li><div class='ball'><span>" + this.idx + "</span></div></li>"),
pos
_this = this;
this.parent.append(_html);
this.html = _html.find(".ball");
this.html.css({
height: Ball.r * 2 + "px",
width: Ball.r * 2 + "px",
lineHeight: Ball.r * 2 + "px"
});
pos = this.html.position()
this.html.css({
left: pos.left + "px",
top: pos.top + "px",
position: "absolute"
});
this.html.mousedown(function() {alert(_this.idx)});
}
};
$("document").ready(function() {
var parent = $("#balls ul"),
libra = 1;
for (var i = 1; i < 13; i++) {
new Ball(i, parent, libra)
}
});
謝謝。已經解決了。 – user1765418