将HTML加载到DIV中,但保留CSS,JS包括

Load HTML into a DIV, but keeping the CSS,JS includes

本文关键字:保留 JS 包括 CSS HTML 加载 DIV      更新时间:2023-09-26

我有一个索引页,我想在div上加载另一个页面从一个非常不同的目录,但我想,页面将被加载到div保持CSS和JS包括,这是可能的吗?



它似乎只加载到DIV,另一个页面的主体,而忘记了JS和CSS包含。

我使用以下代码将HTML加载到div中:

<script type="text/javascript">
            function processAjax(url) { 
                if (window.XMLHttpRequest) { // Non-IE browsers 
                    req = new XMLHttpRequest(); 
                    req.onreadystatechange = targetDiv; 
                    try { 
                        req.open("GET", url, true); 
                        } catch (e) { 
                        alert(e); 
                    } 
                    req.send(null); 
                    } else if (window.ActiveXObject) { // IE 
                    req = new ActiveXObject("Microsoft.XMLHTTP"); 
                    if (req) { 
                        req.onreadystatechange = targetDiv; 
                        req.open("GET", url, true); 
                        req.send(); 
                    } 
                } 
            } 
            function targetDiv() { 
                if (req.readyState == 4) { // Complete 
                    if (req.status == 200) { // OK response 
                        document.getElementById("mContent").innerHTML = req.responseText; 
                        } else { 
                        alert("Problem: " + req.statusText); 
                    } 
                } 
            }  
        </script>



用法:

<a href="javascript:processAjax('./DataTables/extras/Editor/treinamentos/index.html');">My Page</a>

为什么不使用一个IFRAME标签来包含你想要用js和css显示的页面的url呢?

<iframe src="mypage.html"></iframe>