js如何在一頁面加載出另一個頁面的一個table內容
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
![]() ![]() <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div id="get_table_value"></div> </body> <script> // 假設你想要加載的頁面URL是'sourcePageUrl',表格的ID是'tableId' const sourcePageUrl = 'sourcePage.html'; const tableId = 'middleTable'; fetch(sourcePageUrl) .then(response => response.text()) .then(html => { // 解析HTML字符串,創建DOM元素 const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html');
// 從解析后的文檔中獲取表格 const table = doc.getElementById(tableId);
// 檢查表格是否存在 if (table) { // 將表格添加到當前頁面的某個元素中 //document.body.appendChild(table.cloneNode(true) document.getElementById("get_table_value").appendChild(table.cloneNode(true) ); } else { console.error('Table not found'); } }) .catch(error => { console.error('Error fetching the page:', error); });
</script> </html> ?該文章在 2024/12/16 16:35:11 編輯過 |
關鍵字查詢
相關文章
正在查詢... |