这次给大家带来jQuery动态操作div步骤详解,jQuery动态操作div步的注意事项有哪些,下面就是实战案例,一起来看一下。
代码实例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#father
{
width:150px;
height:150px;
background-color:red;
}
#father p
{
width:50px;
height:50px;
background-color:green;
font-size:12px;
}
</style>
<script type="text/javascript" src="/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#father").prepend("<p>本站欢迎您</p>");
})
</script>
</head>
<body>
<p id="father"></p>
</body>
</html>以上代码可以在父p中添加一个p,下面再介绍一下如何删除一个p,下面只给出核心代码:
$(document).ready(function(){
$("#father").prepend("<p>本站欢迎您</p>");
$("p").remove("#father p");
})以上代码可以移除添加的p。
示例二:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add And Close p</title>
<script type="text/javascript教程" >
$(document).ready(function(){
bindListener();
})
function addimg(){
$("#mp").append('<p class="iptp" ><input type="file" name="img[]" class="ipt" /><a href="#" name="rmlink">X</a></p>');
// 为新元素节点添加事件侦听器
bindListener();
}
// 用来绑定事件(使用unbind避免重复绑定)
function bindListener(){
$("a[name=rmlink]").unbind().click(function(){
$(this).parent().remove();
})
}
</script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label>请选择上传的图片</label>
<a href="javascript:addimg()" id="addImg">增加图片</a>
<p class="mp" id="mp">
<p class="iptp" ><input type="file" name="img[]" class="ipt" /><a href="#" name="rmlink">X</a></p>
</p>
<input type="submit" name="submit" value="上传图片" />
</form>
</body>
</html>相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是jQuery动态操作div步骤详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号