jquery中的is和is(":hidden") 【轉載】

來源:互聯網
上載者:User

標籤:des   style   java   os   strong   io   for   cti   

  書到用時方恨少,的確是這樣的,很久都在用jquery了,只是用的時候不是很多,一般用的也就那麼幾個屬性和事件。用的時候需要什麼也是在網上找。以後 還是多看看手冊,多幾點東西。。。這樣用起來的時候就沒有這麼痛苦了。今天用了is 和hidden,給大家分享下,順便自己也加深下印象。

以下內容來自手冊:

:hidden匹配所有的隱藏元素,input 元素的 type 屬性為 "hidden" 的話也會被匹配到傳回值

Array<Element>

樣本

尋找所有不可見的 tr 元素

HTML 程式碼:

<table>
  <tr style="display:none"><td>Value 1</td></tr>
  <tr><td>Value 2</td></tr>
</table>

jQuery 代碼:

$("tr:hidden")

結果:

[ <tr style="display:none"><td>Value 1</td></tr> ]

is(expr)用一個運算式來檢查當前選擇的元素集合,如果其中至少有一個元素符合這個給定的運算式就返回true。如果沒有元素符合,或者運算式無效,都返回‘false‘. ‘filter‘ 內部實際也是在調用這個函數,所以,filter()函數原有的規則在這裡也適用。Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.If no element fits, or the expression is not valid, then the response will be ‘false‘. ‘filter‘ is used internally, therefore all rules that apply there apply here, as well.傳回值

Boolean

參數

expr (String) :用於篩選的運算式

樣本

由於input元素的父元素是一個表單元素,所以返回true。

HTML 程式碼:

<form><input type="checkbox" /></form>

jQuery 代碼:

$("input[type=‘checkbox‘]").parent().is("form")

結果:

true

執行個體:

JavaScript代碼

<script language="javascript" src="jquery.js"></script>   

<script>   

jQuery(function($){   

    $(".abc").click(function(){   

if ($(this).next(".content").is(":hidden")) {   

            $(this).next(".content").fadeIn("slow");   

            $(this).html("收合");   

            $(this).addClass("closeMore");   

        } else {   

            $(this).next(".content").fadeOut("slow");   

            $(this).html("開啟");   

            $(this).addClass("closeMore");         

        }   

    })   

})   

</script> 

XML/HTML代碼

<div>  

<span class="abc">開啟</span>  

<div class="content" style="display: none;">  

    div_1 內容   

</div>  

</div>  

<div>  

<span class="abc">開啟</span>  

<div class="content"  style="display: none;">  

    div_2 內容   

</div>  

</div> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.