Home > Article > Web Front-end > Detailed explanation of jQuery’s prev() usage
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>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 Inp = document.getElementsByTagName("input"); function less(){ //查找元素的上一个元素 并移除 $(".reduce").prev().remove(); if(Inp.length < 1){ $(".reduce").remove() } } $(".reduce") </script>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!