[英]Issue with Javascript Event Handler using a variable outside its scope
我有一個像這樣的javascript事件處理程序:
for (i = 0; i < x; i++){
var table = tableList[i];
var tableID = table.getAttribute('id');
var selector = table.querySelectorAll('input')[0];
selector.on('focusout', function(){
alert(tableID);
}):
}
無論我使用哪個表,tableID警報始終是tableList中最后一個表的ID。
有任何想法嗎?
您可以將tableID作為數據對象附加到事件處理程序。 請嘗試以下方法。
selector.on('focusout', { tableID: tableID }, function(e) {
alert(e.data.tableID);
}):
根據jQuery文檔。
//data
//Type: Anything
//Data to be passed to the handler in event.data when an event is triggered.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.