2014-02-14 71 views
1

我有一個顯示/隱藏div腳本,當用戶單擊以顯示div時也會記錄信息。日誌工作正常,div顯示/隱藏,但只有第二次點擊,它沒有用於在我添加日誌功能之前做到這一點。 我是一個完整的JS/JQuery noob,請解釋我出錯的地方!JQuery需要點擊兩次才能啓動腳本

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script type="text/javascript"> 
function showAnswer(whichLayer,id,lang){ 
    var whichLayer, id, lang; 
    if(document.getElementById(whichLayer).style.display =="none"){ 
    document.getElementById(whichLayer).style.display = "block"; 
    $.post("../../logclick.php", {id: id, lang: lang}); 
    }else{ 
    document.getElementById(whichLayer).style.display = "none"; 
    } 
} 
</script> 

編輯:調用函數的代碼。

<div class="question"> 
    <a href="javascript:showAnswer('2111692','1692','en')">Click here</a> 
</div> 
<div id="2111692" class="answer">Answer shows here.</div> 

樣式表:

.answer { 
clear: both; 
margin-left: 30px; 
display: none; 
} 
+0

這個事件是如何連接起來的? –

+0

編輯帖子以添加呼叫功能。 – Marine

+1

你確定你的'style.display'確實是'none'嗎? – putvande

回答

0

由於使用(和標籤)jQuery的,你可以我們.is(':hidden')更換style.display == 'none'

​​

您需要點擊兩次,因爲第一時間style.display == 'none'評估爲false,因此進入您的else並設置實際上0到none

+0

它的工作原理!謝謝! – Marine

相關問題