首頁 >web前端 >js教程 >jQuery is()函數用法3例_jquery

jQuery is()函數用法3例_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原創
2016-05-16 16:49:391230瀏覽

jQuery提供了is()方法可以很方便的判斷元素是否可見,是否隱藏,是否選取。

一、判斷元素是否隱藏

如下html中的div元素是隱藏的:

複製程式碼 程式碼如下:





   


   


二、判斷checkbox是否選取

jquery中可以用xx.is(':checked')判斷checkbox,radiobutton是否為選取狀態,如下測試html

複製程式碼



複製程式碼

複製程式碼



複製程式碼

程式碼如下:







    chkChecked
 chkChecked chkNoChecked

   
三、判斷是否使用了某個樣式複製程式碼複製程式碼 程式碼如下




は機能紹介です< title><br> <style> // 基本的なスタイルを設定します<br> div { width:60px; height:5px; float:left; <br> border:4px outset; align:center;<br> font-weight:bolder; }<br> .blue { background:blue; } // スタイル 1<br> .red }// スタイル 2 スパン { カラー: ホワイト; }<br> カラー: 赤; マージン: 左; 🎜> </style><br> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> //ここでは jQuery が使用されていることに注意してくださいスクリプト ライブラリ<br></head><br><body><br> <div></div> //最初の div がここに表示されることに注意してください<br><div class=" blue"> ;</div>//2 番目の div がここに表示されることに注意してください<br><div></div>//3 番目の div がここに表示されることに注意してください<BR><div class=" red">< ;/div>//4 番目の div <br> がここに表示されることに注意してください <br><div><br/><span>Peter</span></div>//ここに 5 番目の div が表示されることに注意してください<br><div class="blue"> ;/div>//ここに 6 番目の div が表示されることに注意してください<p> </p><p><script><br> $("div").one('click', function ( ) { //$("div").one は div 要素にイベントをアタッチすることを表します。<br>//クリックやマウスアウトなどの複数のイベントをアタッチして同時に何かを実行することもできます <br> if ( $(this ).is(":first-child")) { //is 関数が機能し、is(":first-child") は <br> を表します。 //この div が最初に出現する div かどうかを判断します<br> $ ("p").text("It's the first div."); // text と html の違いは、html タグをサポートしているかどうかです <br> // このときにアラートを記述すると、実行されません <br> } else if ($(this).is(".blue,.red")) { //div に青クラスがあるか赤クラスがあるかを判断します<br> $("p") .text("This is blue or red Red div");<br> } else if ($(this).is(":contains('Peter')")) { //Peter という単語がdiv<br> $("p" ).text("ピーターだよ!");<br> } else {<br> } $("p").html("特別なものではない<em>/em&gt ;.");<br> }<br> $("p").hide().slideDown("slow"); //これは p<br> の内容をゆっくりと表示するアニメーション効果です $(this ).css({"ボーダースタイル" : "インセット", カーソル:"デフォルト"});<br> });<br></script><br> <br></body><br></html></p> <p></p> </div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>陳述:</span><div>本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn</div></div></div><div class="nphpSytBox"><span>上一篇:<a class="dBlack" title="解決jQuery動態取得手機螢幕高與寬的問題_jquery" href="https://m.php.cn/zh-tw/faq/13113.html">解決jQuery動態取得手機螢幕高與寬的問題_jquery</a></span><span>下一篇:<a class="dBlack" title="解決jQuery動態取得手機螢幕高與寬的問題_jquery" href="https://m.php.cn/zh-tw/faq/13115.html">解決jQuery動態取得手機螢幕高與寬的問題_jquery</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>相關文章</h2><em><a href="https://m.php.cn/zh-tw/article.html" class="bBlack"><i>看更多</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/zh-tw/faq/1609.html" title="深入淺析Bootstrap列表組元件" class="aBlack">深入淺析Bootstrap列表組元件</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/zh-tw/faq/1640.html" title="JavaScript函數柯里化詳解" class="aBlack">JavaScript函數柯里化詳解</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/zh-tw/faq/1949.html" title="JS密碼產生與強度偵測完整實例(附demo源碼下載)" class="aBlack">JS密碼產生與強度偵測完整實例(附demo源碼下載)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/zh-tw/faq/2248.html" title="Angularjs整合微信UI(weui)" class="aBlack">Angularjs整合微信UI(weui)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/zh-tw/faq/2351.html" title="JavaScript快速切換繁體中文和簡體中文的方法及網站支援簡繁體切換的絕招_javascript技巧" class="aBlack">JavaScript快速切換繁體中文和簡體中文的方法及網站支援簡繁體切換的絕招_javascript技巧</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>公益線上PHP培訓,幫助PHP學習者快速成長!</p></div><div class="footermid"><a href="https://m.php.cn/zh-tw/about/us.html">關於我們</a><a href="https://m.php.cn/zh-tw/about/disclaimer.html">免責聲明</a><a href="https://m.php.cn/zh-tw/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>