我正在尝试使用 JavaScript 动态加载菜单,然后在加载后将 onclick 事件侦听器设置为菜单的元素。我似乎无法弄清楚如何做到这一点。我正在通过 AJAX 调用加载菜单,菜单内容加载正常,但是当我像预期的那样单击菜单元素时没有任何反应。
这是我的代码部分,我希望它是原因......
//Load Menu and main images
window.onload = function()
{
load_menu();
}
document.getElementById('menu').onload = function()
{
document.getElementById('projects-button').onclick = load_projects_view();
}
//Load menu function
function load_menu()
{
request = getHTTPObject();
request.onreadystatechange = append_menu;
request.open("GET", "./php/menu.php", true);
request.send(null);
}
//Load Project View
function load_projects_view()
{
request = getHTTPObject();
request.onreadystatechange = append_project_view;
request.open("GET", "./php/project_view.php", true);
request.send(null);
}