Javascript链接系统
Javascript链接系统,javascript,html,hyperlink,Javascript,Html,Hyperlink,我正在编写动态CMS,我没有什么问题: 我想在页面上写一些链接,比如,但是这些链接不能点击。(我有javascript代码将链接页面内容加载到-页面必须是动态的,不能刷新)。 这是代码: function loadPage(page) { $(".main").load("http://" + window.location.hostname + page + "/data.html"); window.history.pushState(null, "Title", "http
我正在编写动态CMS,我没有什么问题: 我想在页面上写一些链接,比如
,但是这些链接不能点击。(我有javascript代码将链接页面内容加载到
-页面必须是动态的,不能刷新)。
这是代码:function loadPage(page) {
$(".main").load("http://" + window.location.hostname + page + "/data.html");
window.history.pushState(null, "Title", "http://" + window.location.hostname + page);
}
菜单中的某些链接:
<a class="menuItem" href="/home">
Home
</a>
可能吗?还是有更好的解决办法
但这些链接不能点击
您确实希望它们是“可点击的”(实际上您明确地添加了一个点击处理程序),您只是不希望它们执行页面导航的默认操作
您可以通过组合使用preventDefault()
和returnfalse
来防止这种情况。大概是这样的:
$("a").click(function (e){ // note the event parameter here
loadPage($(this).attr("href"));
e.preventDefault();
return false;
});
$("a").click(function (e){ // note the event parameter here
loadPage($(this).attr("href"));
e.preventDefault();
return false;
});