我正在尝试使用构造函数。 我编写了一个在运行时创建按钮的函数,并为这些按钮添加了一些方法。
$(function () {
$('#generateNew').on('click', function () {
new VerySpecialButton();
})
var globalNumberCounter = 0,
$holder = $('#buttonHolder');
function VerySpecialButton() {
thisButton = this;
this.number = globalNumberCounter++;
this.specialEvent = function () {
alert(thisButton.number)
}
this.$button = $('<button>')
.text('Button[' + thisButton.number + ']')
.on('click', thisButton.specialEvent)
.appendTo($holder)
}
})
单击任何按钮时,我希望此按钮提醒其编号。但每个按钮都会提醒最后创建的按钮编号。
如果你查看FIDDLE,你会明白我的意思。
有人可以帮我找到我错的地方。
谢谢。