我正在處理一組相當大的數據集,這些數據與頁面上的對象相關,一些對象需要鏈接應用於它們onclick。連接到的鏈接是數據集的一部分,我爲變量linkTarget建立鏈接的字符串,並像這樣應用它。在Javascript中開發獨特的動態onclick事件有困難
if (dataTag[i][3]==true){
if(prepend==undefined || prepend=="undefined"){
var linkTarget=ResultsJSON["targetUrl"];
ele.onclick = function(){
window.open(linkTarget);
};
} else {
var linkTarget=prepend+ResultsJSON["targetUrl"];
ele.onclick = function(){
window.open(linkTarget);
};
}
ele指的是用getElementByID拾取的元素。現在我遇到了很多對象,我遇到的問題是每個對象的onclick都是linkTarget的最後一個值。這全部包含在一個函數中,鏈接目標是一個局部變量,所以我不知道爲什麼。我一直在使用的東西數組一樣
ele.onclick=function(){window.open(linkTarget[linkTarget.length-1]);};
甚至
ele.onclick=function(){window.open(linkTarget.valueOf());};
具有相同的結果嘗試。我現在處於虧損狀態,希望得到任何幫助。
我的蜘蛛感覺告訴我你落在了for-loops陷阱的經典閉合。 – hugomg