使用innerHTML属性显示另一个html文件的内容? [英] Using innerHTML property to display content of another html file?

查看:919
本文介绍了使用innerHTML属性显示另一个html文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的一个元素的innerHTML设置为仅显示纯文本的另一个html文件的内容。这可能吗?我正在考虑的方式如下,但显然它将直接将文本设置为该引用,而不是显示文件的内容。

I am trying to set the innerHTML of one of my elements to the content of another html file that just displays plain text. Is this possible? The way I'm thinking of it is as follows, but obviously it will just set the text to that quote directly instead of displaying the contents of the file.

document.getElementById("myElement").innerHTML = "directory/file.html"


推荐答案

您可以执行 ajax 调用获取文件内容并将其放在html中成功。

You can do an ajax call to get the files contents and put them in the html on success.

var xhr = typeof XMLHttpRequest != 'undefined' ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('get', 'directory/file.html', true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) { 
        document.getElementById("myElement").innerHTML = xhr.responseText;
    } 
}
xhr.send();

或使用jQuery, load()方法是为您做的:

Or using jQuery, the load() method is made for you:

$( "#myElement" ).load( "directory/file.html" );

这篇关于使用innerHTML属性显示另一个html文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆