如何动态添加和删除一个div:
本章节介绍一下介绍一下如何动态的在一个元素添加和删除一个div,希望能够得到举一反三之效。
代码实例如下:
<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="author"content="http://www.51texiao.cn/"/><title>蚂蚁部落</title><styletype="text/CSS">#father{width:150px;height:150px;background-color:red;}#father div{width:50px;height:50px;background-color:green;font-size:12px;}</style><scripttype="text/javascript"src="http://www.softwhy.com/mytest/jQuery/jquery-1.8.3.js"></script><scripttype="text/Javascript">$(document).ready(function(){
$("#father").PRepend("<div>蚂蚁部落欢迎您</div>");
})</script></head><body><divid="father"></div></body></html>
以上代码可以在父div中添加一个div,下面再介绍一下如何删除一个div,下面只给出核心代码:
$(document).ready(function(){
$("#father").prepend("<div>蚂蚁部落欢迎您</div>");
$("div").remove("#father div");
})
以上代码可以移除添加的div。
相关阅读:
1.prepend()函数可以参阅jQuery的prepend()方法一章节。
2.remove()函数可以参阅jQuery的remove()方法一章节。
原文地址是:http://www.51texiao.cn/jqueryjiaocheng/2015/0613/4052.html
最为原始地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=8356