Simple operation code for prev () in jQuery

Source: Internet
Author: User

Simple operation code for prev () in jQuery

The prev () function is used to match the first sibling element in the element set. Only the first sibling element is selected and its child element is ignored. Here we will introduce the simple operations of prev () in jQuery. The specific content is as follows:

A friend gave me a request: click the button to delete the input. If one input is left, click the button to disappear.

It is a simple operation, but if you do not know that jquery has prev (), you may take a lot of detours.

Code:

Html

<Div> <input type = "text" placeholder = "username"> <input type = "text" placeholder = "username"> <input type = "text" type = "hidden "> <input type =" text "type =" hidden "> <a class =" reduce "onclick =" less () ">-</a> </div>

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;  }

JS

<Script src = "jquery-1.7.2.min.js"> </script> <script> var Reduce = document. getElementsByClassName ("reduce"); var indium = document. getElementsByTagName ("input"); function less () {// search for the previous element and remove $ (". reduce "). prev (). remove (); if (Indium. length <1) {$ (". reduce "). remove () }}$ (". reduce ") </script>

Here I am writing it in a mix and it is not standard. I only make one Chestnut, so I will focus on understanding it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.