我想在div中加载网页的内容。这是我的代码:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
单击此项目时,应该将网页加载到内容ID中:
<ul><li><a id="menu_top" href='testing.html'><span>My account</span></a></li></ul>
<div id="content"> </div>
JS:
$("#menu_top").click(function() {
var href = $ (this).attr('href');
alert(href);
$("#content").load(href);
return false;
});
testing.html:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
SUCCESS!
</body>
</html>
不幸的是,以上代码无法正常工作。我已经检查了多次,但是找不到问题。
@Milind Anantwar是正确的-必须具有$(document).ready包装好您的JS:
$(document).ready(function(){
$("#menu_top").click(function() {
var href = $ (this).attr('href');
alert(href);
$("#content").load(href);
return false;
});
});
我还测试了@psicopoo是否提到了UL或OL标签是否影响了它,但似乎在Chrome中却没有。*此外,请确保“ testing.html”是要加载的有效页面。
*如http://api.jquery.com/load/上所述:“由于浏览器安全性的限制,大多数“ Ajax”请求均受同一原始策略的约束;该请求无法从另一个域(子域)成功检索数据,端口或协议。”
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句