当尝试通过链接加载内容时,Jquery load不工作,而是重定向而不是更新
Jquery load isn't working when trying load content through a link, redirects instead of updating
我想使用链接来设置div中的内容
我的HTML代码:
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
</head>
<body>
<a href="dashboard.html">dashboard</a>
<a href="basic-info.html">basic-info</a>
<div id="content"></div>
<script src="scripts/links.js" type="text/javascript"></script>
</body>
</html>
My JS Code:
//inside of links.js
// JavaScript Document
$(document).ready(function() {
$('#content').load("dashboard.html");
});
您需要将事件侦听器绑定到链接,并且还需要防止如下默认操作:
$(document).ready(function() {
$('a').on('click', function(e) {
e.preventDefault();//prevents following the link
$('#content').load( this.href );
});
});
您正在点击指向另一个文件的链接,因此浏览器正在尝试重定向到该文件。
你需要阻止链接的默认行为,并使用它作为JS触发器,用Ajax加载内容。
$('a').click(function(event) {
event.preventDefault();
var url = $(this).attr('href');
$('#content').load(url);
return false;
});
相关文章:
- 使用jquery.load('pageName')方法时,未从应用程序缓存加载Html页
- 动态潜水高度jQuery.Load()不工作
- jQuery .load with variable as url
- jQuery.load()是另一个包含prism.js的页面
- for循环中的jquery.load()不起作用
- jQuery load()回调导致POST而不是GET
- css动画和jquery.load()在移动设备上断断续续
- jquery load()没有加载内容
- JQuery load() 注入到 DIV 中的 PHP 文件能否找到有关此 DIV 的任何信息
- 使用 jQuery .load() 加载的内容不起作用
- JQuery .load() does not load html
- jquery .load() 不加载外部 JS
- jQuery.load();并不总是用图像触发
- 如何在使用 jQuery.load 时获取和更改 url
- 在jQuery .load Ajax之后执行if-statement
- 为什么onload=“;函数“;工作,而jquery.load不工作;t
- Yii ActiveForm:通过jQuery.load加载表单
- JQuery中的JQuery.load已加载页面/snippit
- 如何从使用jquery load()加载的另一个html页面执行在主html文件中编写的脚本
- Jquery.Load不适用于android浏览器,但适用于同一设备上的Firefox移动浏览器