2011-11-05 130 views
2

對於我的生活,我似乎無法破解這一點。我是相當新手使用jQuery的,但我想簡單地說:停止執行e.preventDefault();第二次點擊?

我作用於引導到/mypagename

  1. 在a.nav_hidepanel的第一次點擊,火動畫鏈接#panel,但不要讓鏈接照常運作。但是,添加一個替換類.nav_returnlink,以便我們可以在下一次定位它
  2. 現在,第二次單擊該鏈接時,它具有不同的類,因此我們希望允許它像往常一樣正常工作並向用戶發送to/mypagename

禁用鏈接沒有問題,但return true;似乎不工作!我錯過了明顯的東西嗎?

jQuery(document).ready(function(){ 
    $("a.nav_hidepanel").click(function(e){ 
     e.preventDefault(); 
     $("#panel").animate({marginLeft:"-547px"}, 500); 
     $(this).removeClass('nav_hidepanel'); 
     $(this).addClass('nav_returnlink'); 
    }); 

    $("a.nav_returnlink").click(function(){ 
     return true; 
    }); 
}); 

回答

4

加入Click事件由於元素將首次點擊後,有nav_returnlink類,只需檢查它的存在:

jQuery(document).ready(function(){ 
    $("a.nav_hidepanel").click(function(e){ 
     //This will return true after the first click 
     //and preventDefault won't be called. 
     if(!$(this).hasClass('nav_returnlink')) 
      e.preventDefault(); 

     $("#panel").animate({marginLeft:"-547px"}, 500); 
     $(this).removeClass('nav_hidepanel'); 
     $(this).addClass('nav_returnlink'); 
    }); 
}); 
+0

肯定比我用來消除的代碼更精簡:)我明白爲什麼你已經更具體地瞭解何時防止默認操作。非常感謝! –

0

爲什麼不使用計數器呢?

var clickCount = 0; 
$("a.nav_hidepanel").click(function(e){ 
    if(clickCount == 0) { 
     e.preventDefault(); 
     $("#panel").animate({marginLeft:"-547px"}, 500); 
    } 
    clickCount++; 
}); 
+0

我認爲使用計數器有點笨拙? –

+0

@ user9980是的,我想你可以使用布爾值,但似乎你已經找到了解決方案。 – Marko

1

嘗試在現場functtion使用jQuery

$("a.nav_returnlink").live("click",function(){ 
    return true; 
}); 
+0

這也可以解決問題......但我仍然認爲正確的做法是在完成使用後刪除事件處理程序。 – rlemon

0

你總是可以解除事件處理程序...

var clickCancel = function(event) { 
    event.preventDefault(); 
    $(this).unbind("click",clickCancel); 
    $("#panel").animate({marginLeft:"-547px"}, 500);// you add your animation in this function, it will only be called on the first click.. 

}; 
$("a.nav_hidepanel").bind("click", clickCancel); 

see here

4

這裏是爲什麼你的代碼沒有工作的簡短說明:

  • 你a.nav_hidepanel click事件綁定在了preventDefault 這是確定
  • 你綁定點擊事件到當時不存在的元素a.nav_returnlink。 這是一個問題
  • 在您第一次點擊回調時,您會調用e.preventDefault(),因爲此單擊事件永遠不會被解除綁定,所以此鏈接永遠不會執行它的默認操作。 這又是一個問題

這裏是一個可能的解決方案

jQuery(document).ready(function(){ 
    $("a.nav_hidepanel").click(function(e){ 
    e.preventDefault(); 
    $("#panel").animate({marginLeft:"-547px"}, 500); 
    $(this).removeClass('nav_hidepanel'); 
    $(this).addClass('nav_returnlink'); 
    $(this).unbind('click'); //unbind the click event so this is not called again 
    }); 
    $("a.nav_returnlink").live(function(){ //use a live event to bind to this element, that is available in the future 
     return true; 
    }); 
}); 

另一種解決辦法是在第一個回調新的單擊事件綁定:

jQuery(document).ready(function(){ 
    $("a.nav_hidepanel").click(function(e){ 
    e.preventDefault(); 
    $("#panel").animate({marginLeft:"-547px"}, 500); 
    $(this).removeClass('nav_hidepanel'); 
    $(this).addClass('nav_returnlink'); 
    $(this).unbind('click'); 

    $(this).click(function(){ 
     //do whatever you like to do 
    }); 
    }); 
});