Jquery method to delete the previous element: 1. Use the prev() method to obtain the previous element object of the specified element, syntax "$(selector).prev()"; 2. Use the remove() method to delete The previous element object obtained, the syntax is "previous element object.remove()".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
jquery deletes the previous element
In jquery, if you want to delete the previous element, you first need to get the previous element, this is Need to use prev() method.
prev() method returns the previous sibling element of the selected element. (Sibling elements are elements that share the same parent element.)
To obtain the previous element, you need to use the remove() method to delete it.
Implementation code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src="js/jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#but1").click(function() { $("#box").prev().remove(); }); }); </script> </head> <body> <p>第一段</p> <p>第二段</p> <p>第三段</p> <p id="box">第四段--#box元素</p> <p>第五段</p> <p>第六段</p> <button id="but1">删除 #box元素 前一个元素</button> </body> </html>
Related tutorial recommendations: jQuery video tutorial
The above is the detailed content of How to delete the previous element in jquery. For more information, please follow other related articles on the PHP Chinese website!