我需要在某些网页中不更改页眉和页脚,所以这是我从 stackoverflow.com 复制的简单 ajax 代码。但它不起作用。请帮助我。
<html>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#activities").click(function(){
$("#body").load("activities.html");
});
});
</script>
</head>
<body>
<div id="header">
<a href="#" id="#activities">Activities</a>
This is the header.
</div>
<div id="body">
<p>
This is the body.
<p>
</div>
<div id="footer">
This is the footer.
</div>
</body>
</html>
最佳答案
你需要改变
<a href="#" id="#activities">Activities</a>
到
<a href="#" id="activities">Activities</a>
<!.............^.......................-->
注意:属性“ID”的值中不允许使用字符“#”
然后添加e.preventDefault()
防止浏览器对点击事件的默认 Action
$(document).ready(function() {
$("#activities").click(function(e) {
e.preventDefault();
$("#body").load("activities.html");
});
});
关于javascript - Ajax 不工作。我需要页眉和页脚不要每次都重新加载。你能帮助我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21595520/