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.
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 all disappear.
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>
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>
I wrote a mixture here, not standardized, just one Chestnut, the key is understanding.
Related recommendations:
Usage examples of prev() method in jQuery
10 recommended articles about prev()
10 recommended articles about php prev() function
The above is the detailed content of Detailed explanation of simple operation examples of prev() in jQuery. For more information, please follow other related articles on the PHP Chinese website!