Home > Web Front-end > JS Tutorial > Example analysis on the simple operation code of prev() in jQuery

Example analysis on the simple operation code of prev() in jQuery

小云云
Release: 2017-12-26 15:22:04
Original
1379 people have browsed it

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. This article mainly introduces the simple operation code of prev() in jQuery. It is very good and has reference value. Friends who need it can refer to it. I hope it can help everyone.

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.

Code:

html


<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

I wrote a mixture here, not standardized, just a chestnut, the emphasis is on understanding.

Related recommendations:

Usage examples of prev() method in jQuery

10 recommended articles about prev()

php array function sequence prev()

The above is the detailed content of Example analysis on the simple operation code of prev() in jQuery. 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