问题描述
我正在尝试创建一个网站,而我正试图找出如何加载页面。
I'm trying to create a website, and I'm trying to figure out how to load a page.
例如:
点击导航器Home然后点击屏幕底部它加载页面女巫文本说例如Hello Word!。
You click on the navigator "Home" then a the bottom of the screen It loads a page witch text saying for example "Hello Word!".
有人知道该怎么办吗?我很确定它涉及JavaScript。
Does anybody know what to do? I'm pretty sure It involves JavaScript.
推荐答案
要动态加载内容,您可以使用<$ c $进行AJAX调用c> XMLHttpRequest()。
To dynamically load content, you could make an AJAX call using XMLHttpRequest()
.
在这个例子中,一个url被传递给 loadPage()
function,返回加载的内容。
In this example a url is passed to the loadPage()
function, in which the loaded content is returned.
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
function loadPage(href)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", href, false);
xmlhttp.send();
return xmlhttp.responseText;
}
</script>
</head>
<body>
<div onClick="document.getElementById('bottom').innerHTML =
loadPage('hello-world.html');">Home</div>
<div id="bottom"></div>
</body>
</html>
当点击包含Home文本的div元素时,它会设置div元素的html对于在同一相对位置的hello-world.html文档中找到的内容的底部ID。
When the div element containing text of "Home" is clicked, it sets the html of div element with id of "bottom" to content found in the "hello-world.html" document at the same relative location.
hello-world.html
<p>hello, world</p>
这篇关于如何使用JS加载另一个html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!