2017-04-12 70 views
-2


我正在編程動態CMS,我有點問題: 我想在頁面鏈接上寫如<a href="/products/...">product</a>,但這些鏈接無法點擊。 (我有JavaScript代碼來加載鏈接頁面內容到<div class="main"></div> - 頁面必須是動態的,它不能刷新)。 這是代碼:
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> 

和jQuery:

$("a").click(function(){ 
    loadPage($(this).attr("href")); 
}); 

這可能嗎?或者有更好的解決方案?

回答

0

,但這些鏈接不能點擊

希望他們能夠「點擊」(實際上是你明確添加單擊處理程序),你只是不想讓他們執行其頁面導航的默認操作

您可以通過preventDefault()return false的組合來防止這種情況。類似這樣的:

$("a").click(function (e){ // note the event parameter here 
    loadPage($(this).attr("href")); 
    e.preventDefault(); 
    return false; 
});