我试图阻止默认的链接,因为我动态加载到页面使用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