Jquery中文网 www.jquerycn.cn
Jquery中文网 >  jQuery  >  jquery 教程  >  正文 jQuery动态添加、删除元素的方法

jQuery动态添加、删除元素的方法

发布时间:2014-06-14   编辑:www.jquerycn.cn
添加、删除元素在项目中经常会用到,下面为大家介绍下使用jQuery对元素进行动态添加和删除,具体的实现如下,希望对大家有所帮助
复制代码 代码如下:

<script>
$.schoolFn = {
addItem: function(obj){
$("#itemList").append("<li id='liItrm' class='list-group-item clearfix'><span class='glyphicon glyphicon-resize-vertical sort-handle'></span>"+obj+"<input type='hidden' name='audiences' value='"+obj+"'><button type='button' class='close delete-btn deleteItem' onclick='deleteItem(this);' title='删除'>×</button></li>");
},

removeItem: function(obj){
$("#"+obj).replaceWith("");
}
}
$("#itemAdd").click(function(){
var item=$('#teacherInput').val();
$.schoolFn.addItem(item);
$('#teacherInput').val("");
});
function deleteItem(obj){
var item=$(obj).parents(".list-group-item").attr("id");
$.schoolFn.removeItem(item);
}
</script>

您可能感兴趣的文章:
Jquery节点操作的几点知识
jQuery中对节点进行操作的相关介绍
jQuery动态添加、删除元素的方法
jQuery学习笔记 操作jQuery对象 文档处理
jQuery中需要注意的细节问题小结
jquery 如何动态添加、删除class样式方法介绍
jquery常用技巧及常用方法列表集合
jQuery .attr()和.removeAttr()方法操作元素属性示例
jquery 动态添加、删除class样式的方法
从零开始学习jQuery (三) 管理jQuery包装集

关键词: 添加元素  删除元素   
[关闭]