tôi đã tạo ra hai chức năng để tải quan điểm mở rộng của một tháng trong phần lưu trữ blog của tôi khi đó là liên kết được nhấp:GetElementById có hoạt động trên các phần tử được tạo bởi javascript không?
// Load open view of a month in the Archive section
function loadMonth(date) {
// Remove other open month
removeMonth();
// Hide opening month's link
// Define variable to hold month anchor tag
var monthLink = document.getElementById(date);
monthLink.style.display = "none"; // Hide month anchor
// Define new open month node and its attributes
var openMonth = document.createElement("div");
openMonth.setAttribute("id", "openMonth");
openMonth.innerHTML = "Testing one, two, three.";
// Insert open month
// Define a variable to hold the archive Div node
var archive = document.getElementById("archive");
// Insert the open month in the archive node before it's link
archive.insertBefore(openMonth,monthLink);
return;
}
// Close full view of a month and replace with respective link
function removeMonth() {
// Define global function vars
var archive = document.getElementById("archive"); // Define a var to hold the archive Div node
var openMonth = document.getElementById("openMonth"); // Define var to hold the open month Div node
// Get date of full view month for replacement anchor tag where ID = date
var month = openMonth.getElementsByTagName("span")[0].innerHTML; // Define var to hold the name of the open month
var date = (new Date(month + " 15, 2008").getMonth() + 1); // Define var to hold the numerical equivalent of the month
var year = archive.getElementsByTagName("h3")[0].innerHTML.split(" "); // Define var to hold the year being displayed in the archive
date = year[1] + "" + date; // Change date var to string and insert year
// Remove the open month
archive.removeChild(openMonth);
// Show Link for that month
document.getElementById(date).className = "big"; // Fixes display error when anchor has class firstLink
document.getElementById(date).style.display = "inline"; // Returns link from display "none" state
return;
}
Các chức năng hoạt động khi chạy trên nội dung tĩnh ban đầu, nhưng khi một giây liên kết được nhấp vào trong kho lưu trữ, chúng không làm gì cả. Tôi tự hỏi liệu có lẽ vì các phần tử được tạo bởi các hàm của tôi không thể được gọi bởi document.getElementById. Có lẽ một phương pháp khác nhau để truy cập các nút đó nên được sử dụng, hoặc có thể thay thế "tài liệu" bằng một cái gì đó hoạt động trên các phần tử tạo javascript?
Mọi lời khuyên sẽ được đánh giá cao. Cảm ơn.
Bạn có thể đăng ví dụ làm việc về mã của bạn (ví dụ: một ví dụ hiển thị sự cố) ở đâu đó (ví dụ tại http://pastebin.me/)? Nhìn vào mã của bạn, tôi tin rằng có một quan niệm sai lầm chung trong cách tiếp cận của bạn. – Tomalak