2015-05-02 79 views
0

我有一個div,我試圖用JQuery函數來隱藏;JQuery Hide div不工作

function hideDiv() { 

     $('SearchDiv').hide() 
    } 

這是指按鈕;

<input type="button" style="width:170px; float:right;" value="Search" OnClick="hideDiv()"/> 

該div看起來像這樣;

<div id="SearchDiv" style=" border-bottom: 1px; border-style: solid; border-color: darkgrey; background-color: #F3F3F4; width: 100%; color: #4a3c8c; font-family: Verdana; font-size: 9pt;">TEST</div> 

我已經嘗試使用客戶端ID,將按鈕更改爲一個asp:按鈕,仍然沒有顯示div的運氣!我也瀏覽了論壇,但沒有找到任何適合我的東西。

任何幫助,將不勝感激!

回答

-1

你錯過selector加入selector它工作正常。

請參閱snippest。

function hideDiv() { 
 

 
     $('#SearchDiv').hide() 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="SearchDiv" style=" border-bottom: 1px; border-style: solid; border-color: darkgrey; background-color: #F3F3F4; width: 100%; color: #4a3c8c; font-family: Verdana; font-size: 9pt;">TEST</div> 
 
<input type="button" style="width:170px; float:right;" value="Search" OnClick="hideDiv()"/>

4

你選擇SearchDiv類型的元素,你應該選擇的元素與ID SearchDiv

$("#SearchDiv").hide(); 

有關詳細信息,請參閱documentation for the ID selector

另外,還要考慮在你的代碼粘貼事件(所以還是邏輯上與JS禁用,並且不混合的關注功能),這將消除OnClick一部分,並期待這樣的:

// be sure to put the script tag at the end of your body section. 
$("#SearchDiv").click(function(){ 
    $(this).hide(); 
}); 

這也是最好的練習保持CSS文件(而不是內聯)(或至少在style標籤),它會提高性能,並允許從外部調整。