Jquery動態(tài)添加及刪除頁面節(jié)點元素示例代碼
更新時間:2014年06月16日 17:04:27 投稿:whsnow
這篇文章主要介紹了Jquery如何動態(tài)添加及刪除頁面節(jié)點元素,示例代碼如下,需要的朋友不要錯過
通常我們會遇到選中某個條件,然后添加,累計多個后,再進行執(zhí)行。
廢話不多說,直接上代碼!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Jquery動態(tài)添加及刪除頁面節(jié)點</title>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script><!--引用jqurey庫文件-->
<style>
.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;}
span{cursor:pointer;}
</style>
<script>
$(function(){//頁面加載完畢后執(zhí)行
$("input").click(function(){//添加操作
var getval=$("select").val();//獲取當前選中的select值
$("p").before('<li>'+getval+'<span>X</span></li>');//在p標簽前加入所要生成的代碼
});
});
$("span").live("click",function(){//通過 live() 方法附加的事件處理程序適用于匹配選擇器的當前及未來的元素(比如由腳本創(chuàng)建的新元素)
$(this).parent().remove();//移除當前點擊元素的父元素
});
</script>
</head>
<body>
<div class="container">
<div class="top">
<select>
<option>我是一號</option>
<option>我是二號</option>
<option>我是三號</option>
<option>我是四號</option>
<option>我是五號</option>
</select>
<input value="添 加" type="button"/>
</div>
<div class="add"><p></p></div>
</div>
</body>
</html>
廢話不多說,直接上代碼!
復制代碼 代碼如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Jquery動態(tài)添加及刪除頁面節(jié)點</title>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script><!--引用jqurey庫文件-->
<style>
.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;}
span{cursor:pointer;}
</style>
<script>
$(function(){//頁面加載完畢后執(zhí)行
$("input").click(function(){//添加操作
var getval=$("select").val();//獲取當前選中的select值
$("p").before('<li>'+getval+'<span>X</span></li>');//在p標簽前加入所要生成的代碼
});
});
$("span").live("click",function(){//通過 live() 方法附加的事件處理程序適用于匹配選擇器的當前及未來的元素(比如由腳本創(chuàng)建的新元素)
$(this).parent().remove();//移除當前點擊元素的父元素
});
</script>
</head>
<body>
<div class="container">
<div class="top">
<select>
<option>我是一號</option>
<option>我是二號</option>
<option>我是三號</option>
<option>我是四號</option>
<option>我是五號</option>
</select>
<input value="添 加" type="button"/>
</div>
<div class="add"><p></p></div>
</div>
</body>
</html>
相關文章
IE7瀏覽器窗口大小改變事件執(zhí)行多次bug及IE6/IE7/IE8下resize問題
通過本篇文章主要給大家介紹IE7瀏覽器窗口大小改變事件執(zhí)行多次bug及IE6/IE7/IE8下resize問題,需要的朋友可以一起來學習2015-08-08JQuery 在文檔中查找指定name的元素并移除的實現(xiàn)方法
下面小編就為大家?guī)硪黄狫Query 在文檔中查找指定name的元素并移除的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05jQuery+Cookie實現(xiàn)切換皮膚功能【附源碼下載】
這篇文章主要介紹了jQuery+Cookie實現(xiàn)切換皮膚功能,結合完整實例形式分析了jQuery結合cookie動態(tài)變換頁面元素樣式的相關操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-03-03