Jquery dynamically adds and removes sample code for page node elements

  • 2020-03-30 03:23:04
  • OfStack

Usually we're going to run into a condition that's selected, and then we're going to add, and then we're going to add more, and then we're going to execute it.

Without further ado, go straight to the code!
<!doctype html> 
<meta charset="utf-8"> 
<title>Jquery Dynamically add and remove page nodes </title> 
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script><!-- reference jqurey The library files --> 
.container{ width:1000px; margin:0 auto;} 
.top{ height:25px; line-height:25px;} 
.top select{ width:80px; height:22px; line-height:22px;} 
.top input{ width:56px; height:22px;} 
.add{ line-height:30px;} 
li{ list-style:none;} 
$(function(){//Execute after the page loads
$("input").click(function(){//Add operation
var getval=$("select").val();//Gets the currently selected select value
$("p").before('<li>'+getval+'<span>X</span></li>');//Adds the code to be generated before the p tag
$("span").live("click",function(){//Additional event handlers via the live() method apply to current and future elements of the selector (such as new elements created by the script)
$(this).parent().remove();//Removes the parent of the currently clicked element

<div class="container"> 
<div class="top"> 
<option> I am a number </option> 
<option> I am a no. 2 </option> 
<option> I am no. 3 </option> 
<option> I am number four </option> 
<option> I am a number five </option> 
<input value=" add   add " type="button"/> 
<div class="add"><p></p></div> 

Related articles: