Attribute,appendChild,insertbefore,removeChild随笔
来源:互联网 发布:ubuntu 调整分辨率 编辑:程序博客网 时间:2024/05/20 10:52
1.getAttribute和setAttribute
<div class="demo" id="ABC" title="鼠标"></div><script> var divs = document.getElementById("ABC"); alert(divs.getAttribute("class")); //class="demo" alert(divs.getAttribute("title")); //title="鼠标" divs.setAttribute("id","DEMO"); //class="DEMO"</script>
2.removeChild
divs.removeAttribute("title"); //删除title属性
3.appendChild和 insertbefore
var ul=document.getElementsByTagName(ul);var lis=ul.children;for(var i=0;i<lis.length;i++){ var newspan = document.createElement("span"); newspan.innerHTML=i+1; circle.appendChild(newspan);}在circle div中追加span标签,标签内容为i当前的数字。 实现的效果是在轮播图中ul中添加li小圆圈。
A.append(B) //意思是在A中添加B,B是A的孩子。
A.insertbefore(B,C) //B,C都是A的孩子,都在A中,但是把B插入A中的时候一定是在C的前面位子。
微博发布
<style> ul{ list-style: none; } .box{ margin: 100px auto; width: 600px; height:auto; border:1px solid #ccc; padding: 20px 0; } textarea{ width: 400px; resize: none; } .box li{ width: 400px; line-height: 20px; border-bottom:1px dashed #ccc; margin-left: 50px; } .box li a{ float: right; } </style></head><body><div class="box"> 微博发布: <textarea name="" id="aaa" cols="30" rows="10"></textarea> <button>发布</button> <ul id="ul"> <li>111</li> <li></li> <li></li> </ul></div><script> window.onload=function () { var btn = document.getElementsByTagName("button")[0]; var txt = document.getElementsByTagName("textarea")[0]; var ul = document.createElement("ul"); btn.parentNode.appendChild(ul); btn.onclick = function () { if(txt.value==""){ alert("请输入数字"); return false; } var newli = document.createElement("li"); newli.innerHTML=txt.value+"<a href='javascript:;'>删除</a>"; var lis = ul.children; if(lis.length==0){ ul.appendChild(newli); }else{ ul.insertBefore(newli,lis[0]); } var as =document.getElementsByTagName("a"); for(var i=0;i<as.length;i++){ as[i].onclick=function () { ul.removeChild(this.parentNode); } } } }</script></body>
阅读全文
0 0
- Attribute,appendChild,insertbefore,removeChild随笔
- getAttribute,appendChild,removeChild,replaceChild,insertBefore 自定义函数
- appendChild、removeChild、insertBefore、createElement、cloneNode的区别;
- 动态操作节点(appendChild(),insertBefore(),removeChild(),replaceChild())
- HTML DOM appendChild() 、insertBefore(node, child)、removeChild(child)、replaceChild(node, child)方法使用指南
- [js高手之路] dom常用API【appendChild,insertBefore,removeChild,replaceChild,cloneNode】详解与应用
- insertBefore 与 appendChild
- insertBefore、cloneNode、appendChild...
- appendChild /insertBefore 的错误
- appendChild 与 insertBefore
- js insertBefore insertAfter appendChild
- JS:appendChild()和insertBefore()
- JS appendChild和insertBefore
- appendChild与insertBefore用法比较
- insertBefore()方法 和 appendChild() 方法
- js:appendChild、insertBefore和insertAfter
- 3-DOM操作节点-insertBefore+replaceChild+removeChild
- DOM动态添加HTML节点 appendChild、removeChild
- 堆排序<一> ---------简单结构堆排序
- 用construct2制作ghost shooter
- leetcode DFS
- codeforces gym 101142G Gangsters in Central City
- 求出0~999之间的所有“水仙花数”并输出。“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1+5+3?,则153是一个“水仙花数”。
- Attribute,appendChild,insertbefore,removeChild随笔
- 计蒜客NOIP模拟赛 直线的交点
- C++基础知识
- 仙人掌[cactus]
- poj 3678 Katu Puzzle && POJ 3207 Ikki's Story IV
- Java在进行网络传输的过程中的奇技淫巧
- Unity中Shader LOD控制
- MATLAB中图像的种类及基础知识
- 快捷键 java