我試圖阻止默認的鏈接,因爲我動態加載到頁面使用jQuery的div,併爲每個href的只是名稱的頁面之前(如href=home
,並固定在加載代碼home.php下面)。爲什麼不能防止默認鏈接?
//initialize home page as active on load up and deactivate link
var activePage = $('a[href$="home"]');
var activePageHref = activePage.attr('href');
activePage.attr('href', '#');
$('a').click(function(e) {
e.preventDefault();
//set the page clicked on
var page = $(this).attr('href');
//check to see if there are any extras to url for php
var pageCheck = page.split('?');
//sort page type
if (pageType == null) {
//dynamically load the page in div bodycontent
$('#bodycontent').load(page + '.php');
} else {
$('#bodycontent').load(page + '.php?' + pageCheck[1]);
}
//pull up last disabled link for navigation
var setOld = $('a[href$="' + activePageHref + '"]');
setOld.attr('href', '' + activePageHref + '');
//set the new disabled link for navigation
activePage = page;
//make link inactive
$(this).attr('href', '#');
});
這是確定的,並在最後返回假的,直到我說更多的事情,我需要在點擊事件函數發生,但準確的說,這部分:
//pull up last disabled link for navigation
var setOld = $('a[href$="' + activePageHref + '"]');
setOld.attr('href', '' + activePageHref + '');
//set the new disabled link for navigation
activePage = page;
//make link inactive
$(this).attr('href', '#');
現在電子。 preventDefault();這是從我所理解的正確的方式做我需要發生的事情,阻止整個事情發生在任何鏈接上。我卡住了。我只需要停止默認動作,並使用我已經添加的功能來創建導航。
而且添加,我有一個懸停功能依賴於這種導航的UL認證,但不包括,因爲它不應該造成一個問題,但我可以把它放在這裏,如果需要的。這是該文檔就緒功能中唯一的其他功能。
你在控制檯上看到任何錯誤? – undefined 2012-07-16 01:13:19
我不太確定如何做到這一點。 – Grant 2012-07-16 01:17:07
如果您使用的是Firefox,則可以安裝顯示錯誤的螢火蟲。 – undefined 2012-07-16 01:18:15