>  기사  >  웹 프론트엔드  >  jQuery is() 함수의 사용예에 대한 자세한 설명

jQuery is() 함수의 사용예에 대한 자세한 설명

零下一度
零下一度원래의
2017-06-19 14:22:361296검색

jquery의 is() 메서드는 요소가 표시되는지, 숨겨지는지, 선택되는지 여부를 결정할 수 있습니다.

아래에 예를 들어보겠습니다.

1. 요소가 숨겨져 있는지 확인
다음 HTML의 p 요소가 숨겨져 있습니다.

<!doctype html>
<html>
<head>
<script src="jquery-1.7.2.min.js"></script>
</head>
<body>
    <p id="test" style="display:none">你看不到我</p>
<span ></span>
    <script type="text/
javascript
">
$(&#39;span&#39;).html(&#39;test p is visible? &#39; + $(&#39;#test&#39;).is(&#39;:visible&#39;));
    </script>
</body>
</html>

2. 체크박스가 선택되어 있는지 확인하세요.
jquery에서 xx.is(':checked')를 사용하면 됩니다. 체크박스와 라디오버튼이 선택되었는지 확인하고, 다음과 같이 html을 테스트하세요

<!doctype html>
<html>
<head>
<script src="jquery-1.7.2.min.js"></script>
</head>
<body>
<p>
    <input type="checkbox" name="chkChecked" checked="checked"/> chkChecked
    <input type="checkbox" name="chkNoChecked" /> chkNoChecked
</p>
<span ></span>
    <script type="text/javascript">
$(&#39;span&#39;).html(&#39;chkChecked checked? &#39; + $(&#39;input[name=chkChecked]&#39;).is(&#39;:checked&#39;) + &#39;<br/> &#39;
    +&#39;chkNoChecked checked? &#39; + $(&#39;input[name=chkNoChecked]&#39;).is(&#39;:checked&#39;) );
    </script>
</body>
</html>

3. 특정 스타일이 사용되는지 확인하세요

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>is函数介绍</title>
  <style>  //设置一些基本样式
  p { width:60px; height:60px; margin:5px; float:left;
      border:4px outset; 
background
:green; text-align:center;
      
font-weight
:bolder; cursor:pointer; }
  .blue { background:blue; }  //样式1
  .red { background:red; }//样式2
  span { color:white; font-size:16px; }
  p { color:red; font-weight:bolder; background:yellow;
      margin:3px; clear:left; display:none; }
  </style>
  <script src="jquery-1.9.1.js"></script> //注意 这里使用的是jQuery官方的脚步库
</head>
<body>
  <p></p>  //注意这里出现了第一个p
<p class="blue"></p>//注意这里出现了第2个p
<p></p>//注意这里出现了第3个p
<p class="red"></p>//注意这里出现了第4个p
<p><br/><span>Peter</span></p>//注意这里出现了第5个p
<p class="blue"></p>//注意这里出现了第6个p
<p> 脚本学堂,www.jbxue.com。</p>
<script> 
  $("p").one(&#39;click&#39;, function () {   //$("p").one  代表对p元素附加一个事件,
//还能附加多个事件 譬如click或者mouseout时同时执行一些事情
    if ($(this).is("
:first-child
")) { //is函数发挥作用了,is(":first-child") 代表 
   //判断此p是否第一个出现的p
      $("p").text("It&#39;s the first p."); //text和html区别在于是否支持html标记
  // 此时你在里面写个 alert是不会执行的
    } else if ($(this).is(".blue,.red")) {  //判断该p是否 具有blue或者red的 class
      $("p").text("这是个蓝色或者红色的p");
    } else if ($(this).is(":contains(&#39;Peter&#39;)")) { //判断p中是否存在Peter这个词
      $("p").text("It&#39;s Peter!");
    } else {
      $("p").html("It&#39;s nothing <em>special</em>.");
    }
    $("p").hide().slideDown("slow"); //这是一个动画效果。慢慢展现p的内容
    $(this).css({"
border-style
": "inset", cursor:"default"});
  });
</script>
</body>
</html>
.

위 내용은 jQuery is() 함수의 사용예에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.