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.