javascript每日一练(四)——DOM二
一、DOM的创建,插入,删除
createElement(标签名) appendChild(节点) insertBefore(节点,原有节点) removeChild(节点)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>创建,删除,插入元素</title>
<script>
window.onload = function(){
var oUl = document.getElementById('ul1');
var oUl2 = document.getElementById('ul2');
var oBtn = document.getElementById('btn1');
var oBtn2 = document.getElementById('btn2');
var oBtn3 = document.getElementById('btn3');
var oTxt = document.getElementById('txt1');
oBtn.onclick = function(){
var oLi = document.createElement('li');
oLi.innerHTML = oTxt.value;
oUl.appendChild(oLi);
};
//insertBefore 插入到...之前
oBtn2.onclick = function(){
var oLi = document.createElement('li');
oLi.innerHTML = oTxt.value;
if(oUl.children.length == 0){
oUl.appendChild(oLi);
}else{
oUl.insertBefore(oLi, oUl.children[0]);
}
};
oBtn3.onclick = function(){
oUl2.removeChild(oUl2.children[0]);
};
};
</script>
</head>
<body>
<input id="txt1" type="text" /><button id="btn1">创建元素</button><button id="btn2">插入到第一位</button><button id="btn3">删除元素</button>
<ul id="ul1">
</ul>
<hr />
<ul id="ul2">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</body>
</html>
javascript每日一练(四)——DOM二的更多相关文章
- javascript每日一练(十二)——运动框架
运动框架 可以实现多物体任意值运动 例子: <!doctype html> <html> <head> <meta charset="utf-8&q ...
- javascript每日一练(三)——DOM一
一.Dom基础 childNodes(有兼容问题),children nodeType getAttribute() firstChild,lastChild,previousSilbing,next ...
- javascript每日一练(二)——javascript(函数,数组)
一.函数 什么是函数 function show(){}//不带参数 function show(){return 123;}//不带参数,有返回值 function show(arg1, arg2, ...
- javascript每日一练(十四)——弹性运动
一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- javascript每日一练(十)——运动二:缓冲运动
一.缓冲运动 实现原理:(目标距离-当前距离) / 基数 = 速度(运动距离越大速度越小,运动距离和速度成反比) (500 - oDiv.offsetLeft) / 7 = iSpeed; 需要注意: ...
- javascript每日一练(七)——事件二:键盘事件
一.键盘事件 onkeydown触发, keyCode键盘编码 ctrlKey altKey shiftKey 键盘控制div移动 <!doctype html> <html> ...
- javascript每日一练(一)——javascript基础
一.javascript的组成 ECMAScript DOM BOM 二.变量类型 常见类型有:number, string, boolean, undefined, object, function ...
- javascript每日一练(五)——BOM
一.BOM打开,关闭窗口 window.open(); window.close(); <!doctype html> <html> <head> <meta ...
- javascript每日一练(十三)——运动实例
一.图片放大缩小 <!doctype html> <html> <head> <meta charset="utf-8"> < ...
随机推荐
- Qt setStyleSheet 添加背景色/背景图片(取消背景色,读取本地文件作为背景色)
容易搞定,mainWindow 是一个QWidget.// 设置背景色为蓝色mainWindow.setStyleSheet("background-color:blue;"); ...
- Gartner 认可 Microsoft 为应用程序平台即服务的领导者
对于 Windows Azure 而言,2013 年是了不起的一年.客户使用量每月都创新高:4 月份 Windows Azure 基础结构服务一经正式发布即受到前所未有的青睐,成为重要的里程碑.Gar ...
- Uva 572 Oil Deposits
思路:可以用DFS求解.遍历这个二维数组,没发现一次未被发现的‘@’,便将其作为起点进行搜索.最后的答案,是这个遍历过程中发现了几次为被发现的‘@’ import java.util.*; publi ...
- eclipse @ 注释为何一写就报错
以前一直奇怪,为什么eclipse自动生成的的代码中的@注释不会报错,而我直接写@就会报错 原因其实很简单: eclipse会检查@注释的位置 举个例子:写@Override,直接写会报错,但如果你继 ...
- 【转】 随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比
梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...
- GitHub学习笔记
安装 Ubuntu上安装Git sudo apt-get install git Windows上安装Git msysgit是Windows版的Git.从http://msysgit.github.i ...
- hdu4513之manacher算法
吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) T ...
- windowsphone中获取手机位置信息
首先在界面中加入一个textblock控件以显示信息 using System; using System.Collections.Generic; using System.IO; using Sy ...
- FileDescriptor
FileDescriptor 在java中的java.io包下面 public final class FileDescriptor { ... } 官方的解释: 文件描述符类的实例用作与基础机器有关 ...
- (Problem 21)Amicable numbers
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into ...