JQUERY与DOM 添加 复制 移除 替换 包裹 遍历子元素

1 获取与设置属性


[javascript]  view plain  copy
  1. <span style="font-size:24px;">$(function()  
  2.     {  
  3.   
  4.         var $v = $("p");  
  5.         var $li = $("ul li:eq(1)");  
  6.         //获取属性及获取页面值  
  7.         var pTitle = $v.attr("title");  
  8.           
  9.         alert("before:"+pTitle);  
  10.         var title2 = $li.attr("title");  
  11.   
  12.         var text = $li.text();  
  13.         //设置属性  
  14.         var $vA= $("p");  
  15.         $vA.attr("title","445");      
  16.         alert("after:"+$vA.attr("title"));  
  17.         alert(title2);  
  18.         alert(text);  
  19.   
  20.     });</span>  

2 利用JS新建结点




[javascript]  view plain  copy
  1. $("#button").click(  
  2.         function(){  
  3.           
  4.         var num= document.getElementById("text").value;  
  5.         alert(num);  
  6.         for(var i=0;i<num;i++ )  
  7.         {  
  8.             var input=document.createElement("input");  
  9.               
  10.             input.setAttribute("type""text");  
  11.               
  12.             document.getElementById("div").appendChild(input);  
  13.               
  14.         }  
  15.         }  
  16.         );  

3 利用jquery操作DOM

(1)附加在后面

[javascript]  view plain  copy
  1. //在后面添加  
  2.   
  3. var div=$("#div");  
  4. div.html="";  
  5. //添加第一种方式  
  6. div.append("<input type='text' value='123'>")  
  7. .append("<input type='text' value='123'>");  
  8. //添加第二种方式  
  9. $("<input type='text' value='123'>").appendTo(div)  
  10. alert('22');  
  11.           
(2)附加在前面

[javascript]  view plain  copy
  1. //在前面添加  
  2.         div.prepend("<input type='text' value='before'>");  

(3) 结点的移动

[javascript]  view plain  copy
  1. //插入在XX的后面 让0在3的后面  
  2. $("ul li:eq(0)").insertAfter($("ul li:eq(3)"));  
  3.   
  4. //让3在0后面的后面  
  5. $("ul li:eq(0)").after($("ul li:eq(3)"));  

(4) 结点的移除

[javascript]  view plain  copy
  1. //移除  
  2.           
  3.           
  4.         //(1)第一种方式  
  5.         //删除 并返回被删除的结点对象  (可以间接实现移除)  
  6.         var removed=$("ul li:eq(2)").remove();  
  7.           
  8.         $("ul li:eq(0)").append(removed);  
  9.           
  10.         //(2)第二种方式 按照属性删除  
  11.           
  12.           
  13.         $("ul li").remove("li[title=1]");  
  14.           
  15.           
  16.         // 清空元素内容  
  17.           
  18.         $("ul li:eq(0)").empty();  

示例 动态添加文件上传窗口,动态删除




[javascript]  view plain  copy
  1. <script type="text/javascript">  
  2.     $(function()  
  3.     {  
  4.         var div = $("#div")  
  5.         $("#button").click(function()  
  6.   
  7.         {  
  8.   
  9.             var br = $("<br>");  
  10.             var file = $("<input type='file' />");  
  11.             var btn = $("<input type='button' value='remove'/>");  
  12.             $("#div").append(br).append(file).append(btn);  
  13.   
  14.             btn.click(  
  15.   
  16.             function()  
  17.             {  
  18.                 br.remove();  
  19.                 file.remove();  
  20.                 btn.remove();  
  21.   
  22.             }  
  23.   
  24.             )  
  25.   
  26.           
  27.         }  
  28.   
  29.         )  
  30.   
  31.     })  
  32. </script>  
  33.   
  34.   
  35.   
  36.   
  37.   
  38. </head>  
  39.   
  40. <body>  
  41.     <input type="file" id="file" />  
  42.     <input type="button" value="more" id="button">  
  43.     <div id="div"></div>  
  44. </body>  

(5) 结点的复制

[javascript]  view plain  copy
  1. <script type="text/javascript">  
  2.     $(function()  
  3.     {  
  4.         $("ul li").click(function()  
  5.         {  
  6. //参数true表示克隆出来的对象与原对象拥有相同的属性 方法  
  7.             $(this).clone(true).appendTo($("ul"));  
  8.         });  
  9.     });  
  10. </script>  
  11. </head>  
  12.   
  13. <body>  
  14.     <p title="123">option</p>  
  15.     <ul>  
  16.         <li>0</li>  
  17.         <li title="1">1</li>  
  18.         <li>2</li>  
  19.         <li>3</li>  
  20.     </ul>  
  21. </body>  

(6) 替换结点


[javascript]  view plain  copy
  1. $(function()  
  2. {  
  3.   
  4.   
  5. //后面替换前面的  
  6.  //$("p").replaceWith("<a href='http://www.google.com'>Google.com</a>");  
  7. //前面替换后面的  
  8. $("<a href='http://www.google.com'>Google.com</a>").replaceAll("p");  
  9. });  

(7)元素的包裹


[javascript]  view plain  copy
  1. $(function()  
  2. {//会面包裹前面的  
  3.  //$("p").wrap("<a href='http://www.google.com'><b></b></a>");  
  4. //前面包裹后面的  
  5.     $("p").wrapInner("<a href='http://www.google.com'><b></b></a>");  
  6. });  


(8) 操作CSS



[javascript]  view plain  copy
  1. <style type="text/css">  
  2.   
  3. .high{  
  4. font-weight: bold;  
  5. color:red  
  6. }  
  7.   
  8. .another{  
  9. font-style:italic;  
  10. color:green  
  11. }  
  12.   
  13.   
  14.   
  15. </style>  
  16.   
  17.  <script type="text/javascript" src="jquery-1.4.4.js"></script>  
  18.   
  19. <script type="text/javascript">  
  20.   
  21.  $(function()  
  22.  {  
  23.     $("input:eq(0)").click(function()  
  24.     {  
  25.         alert($('p').attr("class"));  
  26.     });  
  27.   
  28.     $("input:eq(1)").click(function()  
  29.     {  
  30.         $("p").attr("class""high");  
  31.     });  
  32.   
  33.     $("input:eq(2)").click(function()  
  34.     {  
  35.         $("p").addClass("high");  
  36.     });  
  37.   
  38.     $("input:eq(3)").click(function()  
  39.     {  
  40.         $("p").removeClass("high");  
  41.     });  
  42.   
  43.     $("input:eq(4)").click(function()  
  44.     {  
  45.         $("p").removeClass();  
  46.     });  
  47.   
  48.     $("input:eq(5)").click(function()  
  49.     {  
  50.         //两种状态切换  添加与删除  
  51.         $("p").toggleClass("another");  
  52.     });  
  53.   
  54.     $("input:eq(6)").click(function()  
  55.     {  
  56.     //判断元素是否具有制定样式  
  57.         //alert($('p').hasClass('another'));  
  58.   
  59.         alert($('p').is('.another'));  
  60.     });  
  61.   
  62.   
  63.   
  64.  });  

(10) 对按钮内容的修饰

[javascript]  view plain  copy
  1. $(function()  
  2.  {  
  3.     $("input:eq(0)").click(function()  
  4.     {  
  5.         alert($("p").html());  
  6.     });  
  7.   
  8.     $("input:eq(1)").click(function()  
  9.     {  
  10.         alert($("p").text());  
  11.     });  
  12.   
  13.     $("input:eq(2)").click(function()  
  14.     {  
  15.         $("p").html('<a href="http://www.google.com">hello world</a>');  
  16.     });  
  17.   
  18.     $("input:eq(3)").click(function()  
  19.     {  
  20.         $("p").text('<a href="http://www.google.com">hello world</a>');  
  21.     });  
  22.   
  23.     $("input:eq(4)").click(function()  
  24.     {  
  25.         alert($(this).val());  
  26.     });  
  27.   
  28.     $("input:eq(5)").click(function()  
  29.     {  
  30.         $(this).val('hello world');  
  31.     });  
  32.  });  

利用 focus和blur事件实现输入框默认值显隐

[javascript]  view plain  copy
  1. $("#username").focus(function()  
  2. {  
  3.     if($(this).val()=="username")  
  4.     {  
  5.     $(this).val("");  
  6.     }  
  7. }).blur(function()  
  8. {  
  9.   
  10.     if($(this).val()=="")  
  11.       
  12.     {  
  13.     $(this).val("username");  
  14.     }  


11  对子元素进行遍历

[javascript]  view plain  copy
  1. $(function()  
  2. {  
  3.     var v1 = $("body").children();  
  4.     var v2 = $("p").children();  
  5.     var v3 = $("ul").children();  
  6.       
  7.     alert(v1.length);  
  8.     alert(v2.length);  
  9.     alert(v3.length);  
  10.   
  11.     for(var i = 0; i < v3.length; i++)  
  12.     {  
  13.         alert(v3[i].innerHTML);  
  14.     }  
  15.   
  16. }  







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值