使用jQuery而不是html框架从菜单栏显示所选页面,而无需在同一页面中重新加载
由于HTML框架已经过时,我想使用JQuery。这可以通过任何其他方法实现。可以使用任何AJAX实现。jQuery就是这样一种实现,它可以异步加载页面内容,而无需重新加载整个页面 使用jQuery实现此功能的基本示例如下:使用jQuery而不是html框架从菜单栏显示所选页面,而无需在同一页面中重新加载,jquery,Jquery,由于HTML框架已经过时,我想使用JQuery。这可以通过任何其他方法实现。可以使用任何AJAX实现。jQuery就是这样一种实现,它可以异步加载页面内容,而无需重新加载整个页面 使用jQuery实现此功能的基本示例如下: <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page Title</title>
</head>
<body>
<div id="menu">
<ul>
<li><a href="/page1">Page 1</a></li>
<li><a href="/page2">Page 2</a></li>
<li><a href="/page3">Page 3</a></li>
</ul>
</div>
<div id="content">
<h1>Welcome</h1>
<p>Welcome page....</p>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$( document ).ready(function(){
// When you click on an anchor in the #menu
$("#menu a").click(function(event){
event.preventDefault()
//Get the content from the page in the anchor herf
$.ajax({
url:$(this).attr('href'),
method:'POST',
async:true,
complete: function(xhr){
//set the innerHTML of #content with the file content
$('#content').html(xhr.responseText);
}
});
});
});
</script>
</body>
</html>
页面标题
欢迎
欢迎页面
$(文档).ready(函数(){
//单击#菜单中的定位点时
$(“#菜单a”)。单击(功能(事件){
event.preventDefault()
//从锚herf中的页面获取内容
$.ajax({
url:$(this.attr('href'),
方法:'POST',
async:true,
完成:函数(xhr){
//将#内容的innerHTML设置为文件内容
$('#content').html(xhr.responseText);
}
});
});
});
然后,在页面文件中,您将只在ajax调用的目标中包含要加载的内容:
<h1>Page 1</h1>
<p>This is page 1...</p>
第1页
这是第一页
您可以使用jQuery$.get()
或$.ajax()
检索远程资源,然后处理和呈现页面上任何位置的响应。你会在网上找到很多例子。非常感谢。正是我想要的。