js動態(tài)加載另一個頁面的內(nèi)容
當(dāng)前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
![]() ![]() function get_table_value(){ const sourcePageUrl = 'product_data_management_checkinfo.asp?product_code=<%=product_code%>'; const tableId = 'middleTable';
fetch(sourcePageUrl) .then(response => { // 檢查響應(yīng)的內(nèi)容類型,獲取字符編碼 const contentType = response.headers.get("Content-Type"); const encoding = contentType && contentType.includes("charset=") ? contentType.split("charset=")[1] : "gb2312"; return response.arrayBuffer().then(buffer => { // 創(chuàng)建一個 TextDecoder 實例,使用響應(yīng)中的編碼 const decoder = new TextDecoder(encoding); return decoder.decode(buffer); }); }) .then(html => { // 解析HTML字符串,創(chuàng)建DOM元素 const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); // 從解析后的文檔中獲取表格 const table = doc.getElementById(tableId); // 檢查表格是否存在 if (table) { // 將表格添加到當(dāng)前頁面的某個元素中 document.getElementById("get_table_value").appendChild(table.cloneNode(true)); } else { console.error('Table not found'); } }) .catch(error => { console.error('Error fetching the page:', error); }); } 該文章在 2024/12/16 16:54:07 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |