加载HTML到一个DIV,但保持CSS,JS包括加载、HTML、DIV、CSS

2023-09-10 20:47:23 作者:活物【】Rou

我有一个索引页,我想加载一个DIV从一个非常不同的目录中的另一页,但我想,这将被加载到DIV的页面,以保持CSS和JS包括,是否有可能?_爱

它似乎只加载到DIV,其他页面的主体而忘记了JS和CSS包括。

我用这个code加载HTML到DIV:

 <脚本类型=文/ JavaScript的>
            功能processAjax(URL){

                如果(window.XMLHtt prequest){//非IE浏览器
                    REQ =新XMLHtt prequest();
                    req.onreadystatechange = targetDiv;
                    尝试 {
                        req.open(GET,URL,真正的);
                        }赶上(五){
                        警报(E);
                    }
                    req.send(空);
                    }否则,如果(window.ActiveXObject){// IE
                    REQ =新的ActiveXObject(Microsoft.XMLHTTP);
                    如果(REQ){
                        req.onreadystatechange = targetDiv;
                        req.open(GET,URL,真正的);
                        req.send();

                    }
                }
            }

            传播targetDiv(){
                如果(req.readyState == 4){//完整
                    如果(req.status == 200){// OK响应
                        的document.getElementById(mContent)的innerHTML = req.responseText。
                        } 其他 {
                        警报(问题:+ req.statusText);
                    }
                }
            }
        < / SCRIPT>
 

用法:

 < A HREF =JavaScript的:processAjax(./数据表/演员/编辑/ treinamentos / index.html的');>我的页面< / A>
 

解决方案

你为什么不使用包含你的愿望,以显示与您要使用的JS和CSS页面的URL的IFRAME标签。

 < IFRAME SRC =的mypage.html>< / IFRAME>
 
零基础html5 div css js网页开发教程第001期

I've got an index page and I want to load on a DIV another page from a very different directory, but I want that the page that will be loaded into the DIV to keep the CSS and JS includes, is it possible?

It seems that it only loads into the DIV, the body of the other page and forget the JS and CSS includes.

I'm using this code to load the HTML into the 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>

Usage:

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

解决方案

Why don't you use an IFRAME tag that contains the url of the page you desire to display with the js and css you want to use.

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

 
精彩推荐
图片推荐