Home > Web Front-end > JS Tutorial > Detailed explanation of usage examples of jQuery is() function

Detailed explanation of usage examples of jQuery is() function

零下一度
Release: 2017-06-19 14:22:36
Original
1371 people have browsed it

The is() method in jquery can determine whether an element is visible, hidden, or selected.

Below we introduce examples respectively.

1. Determine whether the element is hidden
The p element in the following html is hidden:

<!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>
Copy after login

2. Determine whether the checkbox is selected
You can pass xx in jquery. is(':checked') determines whether checkbox and radiobutton are selected, as follows to test 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>
Copy after login

3. Determine whether a certain style is used

<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>
Copy after login

The above is the detailed content of Detailed explanation of usage examples of jQuery is() function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template