1
我有一個javascript ajax函數,它從我的數據庫中獲取一些信息,並從這些信息創建具有標題和img源的新li元素。每個圖像在創建時也會被賦予一個id。在ajax調用後無法設置null的屬性'onclick'
我的問題是,我希望能夠點擊每個圖像來觸發一個功能,但是當我嘗試通過它的ID訪問每個圖像我得到一個「無法設置屬性onlick'的空錯誤。控制檯
下面是代碼:
的Ajax功能:
function loadNavbar() {
//Load layer list from WRL.
var navbar_list = new Nfis.Ajax.Wrl('carouselNavBarLocal',
"http://local.ol.nfis.org/wrl/resource", {
'domain' : 'nfis'
});
var length = navbar_list.length;
navbar_list.events.register("onResponse", this, function() {
var wrNavbar_List = navbar_list.getList();
var counter = 1;
//Grab the ul and li elements from the HTML Page
var ul=document.getElementById('ul-navbar');
//For each navbar element in the wrl list, we need to create and
// set the map title and image source.
for (var i in wrNavbar_List) {
//we also need to grab the associated namespace and map title
// for loading the overlay_map layers and changing the title later.
namespaces.push(wrNavbar_List[i].getUrl2());
map_titles.push(wrNavbar_List[i].getTitle(lang));
//Creation and Setting of title and image source for each li element.
var navbar_h3 = document.createElement('h3');
var navbar_img = document.createElement('img');
navbar_h3.innerHTML = wrNavbar_List[i].getTitle(lang);
navbar_img.src = wrNavbar_List[i].getUrl();
navbar_img.id = "test"+counter;
counter++;
//Creation of each li element
var li = document.createElement('li');
li.appendChild(navbar_h3);
li.appendChild(navbar_img);
ul.appendChild(li);
}
});
//
//navbar_list.fetchList();
}
在我的腳本的底部是的onclick功能:
//Two tests for testing loading in forest cover overlay layers
// and removeMapLayers() function
document.getElementById('test1').onclick = function() {
alert('test');
if (lang === 'fr') {
changeMapTitle('Carte de la couverture forestière');
} else {
changeMapTitle('Canada Forest Cover Map');
}
loadOverlayLayers('forestCoverLayersLocal');
reloadMap();
}
document.getElementById('test3').onclick = function() {
if (lang === 'fr') {
changeMapTitle('Test des couches de base');
} else {
changeMapTitle('Canada Forest Cover Map');
}
removeOverlayLayers(mapLayers);
}
我已經花了大部分時間試圖解決這個問題無濟於事。事情我已經嘗試過window.onload(),回調和$(document).ready(function)之類的東西。請幫幫我!