简体   繁体   English

从另一个页面加载Div内容

[英]Load Div Content from Another Page

I am trying to load a div content from another page(this page is in another project which is running in tomcat all together) with javascript ajax. 我正在尝试使用javascript ajax从另一个页面(该页面在tomcat中一起运行的另一个项目)中加载div内容。

 var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:8080/prob-services/clogin#login_page');
xhr.onload = function() {
alert(xhr.status);
console.log(xhr);
    if (xhr.status === 200) {
        var modal = document.getElementById("modal_dialog");
        modal.innerHTML = xhr.responseText;
    }
};
xhr.send();

The problem is when I log xhr I see that responseURL is until # , so ajax takes only http://localhost:8080/prob-services/clogin instead of http://localhost:8080/prob-services/clogin#login_page . 问题是当我登录xhr我看到responseURL一直到#为止,因此ajax仅采用http://localhost:8080/prob-services/clogin而不是http://localhost:8080/prob-services/clogin#login_page That's why it loads whole page. 这就是为什么它会加载整个页面。 Is there any way to get only div content without JQuery? 有没有办法在没有JQuery的情况下仅获取div内容?

Solution to filter the whole page: 筛选整个页面的解决方案:

Create a dummy hidden DOM element like 创建一个虚拟的隐藏DOM元素,例如

<div style='display: none' id="loadedHTML"></div>

then put the HTML of the whole page you received from the server into it: 然后将您从服务器收到的整个页面的HTML放入其中:

document.getElementById('loadedHTML').innerHTML = xhr.responseText;

now you can search the html inside this component. 现在您可以搜索此组件内的html。 Say the part you want to is in a div called "MyDiv" inside the page: 假设您想要的部分位于页面内名为“ MyDiv”的div中:

var modal = document.getElementById("modal_dialog");
modal.innerHTML = document.getElementById('MyDiv').innerHTML;

fell free to ask more if it's not clear enough. 可以自由地询问更多,如果还不够清楚。

Use this jQuery load() function 使用此jQuery load()函数

$('#modal_dialog').css('opacity','1').load('http://localhost:8080/prob-services/clogin',function() { 
    alert():
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM