Javascript 在html中包含菜单
如何在index.html中包含menu.html中的菜单Javascript 在html中包含菜单,javascript,jquery,Javascript,Jquery,如何在index.html中包含menu.html中的菜单 <!doctype html> <html> <head> <title>load demo</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </hea
<!doctype html>
<html>
<head>
<title>load demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<b>Projects:</b>
<ul id="new-projects"></ul>
<script>
$( "#new-projects" ).load("menu.html");
</script>
</body>
</html>
在menu.html中,我只有
<li><a href="home.html">Home</a></li>
<li><a href="news.html">News</a></li>
在index.html中
<!doctype html>
<html>
<head>
<title>load demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<b>Projects:</b>
<ul id="new-projects"></ul>
<script>
$( "#new-projects" ).load("menu.html");
</script>
</body>
</html>
加载演示
项目:
$(“#新项目”).load(“menu.html”);
这里出了什么问题?您需要跟踪浏览器日志:
$( "#new-projects" ).load("menu.html", function( r, s, x ) {
console.log(r);
console.log(s);
console.log(x);
});
这一失败有多具体?看看你的浏览器的调试工具。是否正在发出AJAX请求?服务器是否使用预期内容进行响应?JavaScript控制台上是否有任何错误或信息?(一个常见的初学者错误是,您可能根本没有使用服务器。是吗?还是您只是从文件系统打开HTML文件?如果是后者,则AJAX请求根本不起作用。)这两个文件是否放在同一文件夹中?
您缺少一个
打开标记。@Jai I have all tag,两个html页面都在一个页面中folder@doe_le:当您说“使用Chrome或Opera打开index.html”时,您的意思是您只是从文件系统而不是从web服务器打开文件吗?因为我们已经(两次)提到AJAX在这种情况下不起作用。再说一次,“不工作”并不能描述问题。在Chrome中打开开发者工具并查看其中的信息。看起来我们好像是在你的电脑里跟你说话,但我向你保证我们不是。我们实际上看不到那里发生了什么。对不起,我的名声还不到50!现在我可以发表评论了:P