【发布时间】:2016-05-12 07:06:12
【问题描述】:
我使用 jquery 加载方法作为 iframe 的替代方法,将 html 内容加载到我的 index.html(home) 中的 div 中,并且我得到了包含链接的静态导航栏。但是在单击第一个链接后,它似乎不再起作用了。我还得到了一个控制台日志:
主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。如需更多帮助,请查看https://xhr.spec.whatwg.org/。
PS:初学者级别:)
这是我的代码
HTML 链接:
<ul class="dropdown-menu">
<li id="tolink1"><a href="#">SAMPLE 1</a></li>
<li id="tolink2"><a href="#">SAMPLE 2/a> </li>
<li id="tolink3"><a href="#">SAMPLE 3</a></li>
<li id="tolink4"><a href="#">SAMPLE 4</a></li>
</ul>
jquery:
//#loadcontent is the id of the div
$(document).on('click', '#tolink1', function(e){
$("#loadContent").load('http://localhost/impex/SAMPLE1.html');
});
$(document).on('click', '#tolink2', function(e){
$("#loadContent").load('http://localhost/impex/SAMPLE2.html');
});
$(document).on('click', '#tolink3', function(e){
$("#loadContent").load('http://localhost/impex/SAMPLE3.html');
});
$(document).on('click', '#tolink4', function(e){
$("#loadContent").load('http://localhost/impex/SAMPLE4.html');
});
【问题讨论】:
-
你为什么使用
e.preventDefault()? -
哎呀!我刚刚在我的代码上尝试了一些东西,但我已经删除了。
-
现在可以用了吗?
-
是的,链接有效,但仅在第一次之后我无法再点击任何链接。我还注意到我浏览器上方的网址类似于 localhost/impex/#
-
我使用 jquery 将外部 html 页面加载到包含链接的 div 主体中。具有 .click(function()) 和 .load() 行为的 Jquery 无法激活这些链接。但是,通过使用 "click to link" "$(document).on('click', '#tolink4', function(e){ }",它成功激活了这些链接。谢谢。