onklick綁定行在3個div上運行,但是它們全部在單擊時警告最後一個設置值i
。我希望我所做的事情是有道理的,這很難解釋。它會警告3,3,3,而不是提醒1,2或3。jquery無法繞過這個
// Updates bar preview box
this.updatePropertyMarkerBox = function(self, BarsID) {
... snip ...
// Loop and add event handler
for (var i = 0; i < self.bars[BarsIndex].markers.length; i++) {
// Add click event
$("#bmi-" + self.containerId + "-" + i).bind('click', function() {
alert(i);
});
}
的可能重複[事件不會被添加一個for循環(http://stackoverflow.com/questions/6332589/event-doesnt-get -ad-in-a-for-loop) –
剛剛回答完全一樣的東西:http://stackoverflow.com/questions/6492564/webkit-executesql-sentence-and-loop-problem/ – davin
@Felix:你的意思是之前曾被問過? ; o) – user113716