Insert a <span> element after each <p> element:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//from www . j ava 2 s . c o m <script> $(document).ready(function(){ $("button").click(function(){ $("<span>Hello world!</span>").insertAfter("p"); }); }); </script> </head> <body> <button>Insert span element after each p element</button> <p>This is a paragraph.</p> <p>This is another paragraph.</p> </body> </html>
The insertAfter()
method inserts HTML elements after the selected elements.
$(content).insertAfter(selector)
Parameter | Optional | Description |
---|---|---|
content | Required. | the content to insert. If content is an existing element, it will be moved from its current position. |
selector | Required. | where to insert the content |