Home > Web Front-end > JS Tutorial > Detailed explanation of jQuery's prev() usage

Detailed explanation of jQuery's prev() usage

php中世界最好的语言
Release: 2018-04-19 14:21:51
Original
4064 people have browsed it

This time I will bring you a detailed explanation of the use of jQuery's prev(). What are the precautions for using jQuery's prev()? The following is a practical case, let's take a look.

prev() function is used to match the previous sibling element of the element set. Only the previous sibling element is selected, and its child elements will be ignored. Here I will introduce to you the simple operation of prev() in jQuery. The specific content is as follows:

A friend gave me a request: click the button to delete the input. If there is only one input left, click the button to delete all inputs.

It's a very simple operation, but if you don't know that there is a prev() method in jquery, you may take a lot of detours.

<p>
  <input type="text" placeholder="用户名">
  <input type="text" placeholder="用户名">
  <input type="text" type="hidden">
  <input type="text" type="hidden">
  <a class="reduce" onclick="less()">—</a>
 </p>
Copy after login
css

.reduce{
   display: inline-block;
   color: white;
   text-align: center;
   width: 30px;
   height: 30px;
   background: red;
   line-height: 30px;
   cursor: pointer;
  }
  input{
   height: 18px;
   padding: 5px 10px;
  }
Copy after login
JS

<script src="jquery-1.7.2.min.js"></script>
<script>
 var Reduce = document.getElementsByClassName("reduce");
 var Inp = document.getElementsByTagName("input");
 function less(){
    //查找元素的上一个元素 并移除
  $(".reduce").prev().remove();
  if(Inp.length < 1){
   $(".reduce").remove()
  }
 }
 $(".reduce")
</script>
Copy after login
What I wrote here is mixed and non-standard, just a chestnut, the emphasis is on understanding.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:



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

Related labels:
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