当前位置:Gxlcms > JavaScript > jQuery动态操作div步骤详解

jQuery动态操作div步骤详解

时间:2021-07-01 10:21:17 帮助过:2人阅读

这次给大家带来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>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

jQuery解析xml文件详解

jQuery动态加载js脚本文件实现方法

以上就是jQuery动态操作div步骤详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行