prev() 函數被用來匹配元素集的前一個兄弟元素,僅僅只有前一個兄弟元素被選擇,其子元素將被忽略。這里給大家介紹jQuery里prev()的簡單操作,具體內容如下:
一個朋友提了個需求給我:點擊按鈕刪除input,如果input剩一個,點擊按鈕全部消失。
很簡單的操作,但是你如果不知道jquery里有prev()這個方法,可能你會走很多彎路。
代碼:
html
<div> <input type="text" placeholder="用戶名"> <input type="text" placeholder="用戶名"> <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 Inp = document.getElementsByTagName("input"); function less(){ //查找元素的上一個元素 并移除 $(".reduce").prev().remove(); if(Inp.length < 1){ $(".reduce").remove() } } $(".reduce")</script>這里我混合寫的,不規范,只做一個栗子,重在理解。
總結
以上所述是小編給大家介紹的關于jQuery里prev()的簡單操作代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對錯新站長站網站的支持!
新聞熱點
疑難解答
圖片精選