- 带有Element和不带的区别
a) 带Element的获取的是元素节点
b) 不带可能获取文本节点和属性节点
- 获取所以子节点
a) . childNodes
b) . children
c) . parentNode . children [索引]
- . children:获取所有子节点
- opacity:0-1;透明度
- alpha(opacity:百分数);IE6/7/8透明度
- 先绑定事件再进行循环
- previousNode()上一个兄弟节点
- alert(变量名)可测试变量名是否可以使用
- 获取body,var body = document.body;
设置样式的两种方式
style
- 行内式可以获取打印出来
- 内嵌和外链的获取不了
- 样式少的时候使用
- 驼峰命名规则
- style属性是对象属性
- 值是字符串类型,没设置的时候是“空字符串”
- .style. cssText = “字符串形式的样式”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {
text-decoration: none;
}#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {
text-decoration: none;
}#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {
text-decoration: none;
}#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {
text-decoration: none;
}
text-decoration: none;
}
</style>
<script src="tools.js"></script>
<script>
window.onload = function () {
//需求:点击a链接,让a链接对应的li标签添加类。
//思路:点击a链接,让他的父亲添加类,其他的li标签类名清空。
//步骤:
//1.获取事件源
//2.绑定事件
//3.书写事件驱动程序
//1.获取事件源
var ul = getEle("list");
var aArr = ul.getElementsByTagName("a");
for(var i=0;i<aArr.length;i++){
aArr[i].onclick = function () {
//点击哪个a链接,让他的父亲添加类
this.parentNode.className = "current";
//设置他的父元素的其他所有兄弟节点类名为空
var otherArr = getAllSiblings(this.parentNode);
for(var j=0;j<otherArr.length;j++){
otherArr[j].className = "";
}
}
}
// //1.获取事件源
// var ul = getEle("list");
// var liArr = ul.children;
// //2.绑定事件
// for(var i=0;i<liArr.length;i++){
// var a = getFirstNode(liArr[i]);
// a.onclick = function () {
// //3.书写事件驱动程序
// //排他思想
// for(var j=0;j<liArr.length;j++){
// liArr[j].className = "";
// }
// this.parentNode.className = "current";
// }
// }
}
</script>
</head>
<body>
<div id="menu">
<ul id="list">
<li class="current"><a href="#">首页</a></li>
<li><a href="javascript:void(0)">播客</a></li>
<li><a href="javascript:void(0)">博客</a></li>
<li><a href="javascript:void(0)">相册</a></li>
<li><a href="javascript:void(0)">关于</a></li>
<li><a href="javascript:void(0)">帮助</a></li>
</ul>
</div>
</body>
</html> 菜单的简单练习<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {
text-decoration: none;
}#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {
text-decoration: none;
}#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {
text-decoration: none;
}#list li {
list-style-type: none;
width: 80px;
height: 30px;
line-height: 30px;
background-color:beige;
text-align: center;
float: left;
margin-left: 5px;
}
#list li.current {
background-color: burlywood;
}
#list li a {
text-decoration: none;
}
text-decoration: none;
}
</style>
<script src="tools.js"></script>
<script>
window.onload = function () {
//需求:点击a链接,让a链接对应的li标签添加类。
//思路:点击a链接,让他的父亲添加类,其他的li标签类名清空。
//步骤:
//1.获取事件源
//2.绑定事件
//3.书写事件驱动程序
//1.获取事件源
var ul = getEle("list");
var aArr = ul.getElementsByTagName("a");
for(var i=0;i<aArr.length;i++){
aArr[i].onclick = function () {
//点击哪个a链接,让他的父亲添加类
this.parentNode.className = "current";
//设置他的父元素的其他所有兄弟节点类名为空
var otherArr = getAllSiblings(this.parentNode);
for(var j=0;j<otherArr.length;j++){
otherArr[j].className = "";
}
}
}
// //1.获取事件源
// var ul = getEle("list");
// var liArr = ul.children;
// //2.绑定事件
// for(var i=0;i<liArr.length;i++){
// var a = getFirstNode(liArr[i]);
// a.onclick = function () {
// //3.书写事件驱动程序
// //排他思想
// for(var j=0;j<liArr.length;j++){
// liArr[j].className = "";
// }
// this.parentNode.className = "current";
// }
// }
}
</script>
</head>
<body>
<div id="menu">
<ul id="list">
<li class="current"><a href="#">首页</a></li>
<li><a href="javascript:void(0)">播客</a></li>
<li><a href="javascript:void(0)">博客</a></li>
<li><a href="javascript:void(0)">相册</a></li>
<li><a href="javascript:void(0)">关于</a></li>
<li><a href="javascript:void(0)">帮助</a></li>
</ul>
</div>
</body>
</html> style属性的练习<body>
<div style="width: 100px;height: 100px;background-color: pink;"></div>
<script>
var div = document.getElementsByTagName("div")[0];
div.onmouseover = function () {
div.style.width = "200px";
div.style.height = "200px";
div.style.backgroundColor = "black";
div.style.opacity = "0.2";
div.style.filter = "alpha(opacity=20)";
}
</script>
</body> 改变盒子的大小和透明度<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
input {
display: block;
}
</style>
</head>
<body>
<ul>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<input type="text"/>
<button>设置</button>
</ul>
<script>
//需求:点击设置按钮,让所有的input标签获取焦点后高亮显示
//步骤:
//1.获取事件源
//2.绑定事件
//3.书写事件驱动程序
//1.获取事件源
var inpArr = document.getElementsByTagName("input");
var button = inpArr[inpArr.length-1].nextElementSibling || inpArr[inpArr.length-1].nextSibling ;
//2.绑定事件
button.onclick = function () {
//3.书写事件驱动程序
for(var i=0;i<inpArr.length;i++){
//当button按钮被点击以后,所有的input标签被绑定事件,onfocus事件
inpArr[i].onfocus = function () {
this.style.border = "2px solid red";
this.style.backgroundColor = "#ccc";
}
//绑定onblur事件,取消样式
inpArr[i].onblur = function () {
this.style.border = "";
this.style.backgroundColor = "";
}
}
}
</script>
</body>
</html> 文本框获取焦点高亮显示<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
text-align: center;
}
.wrap {
width: 500px;
margin: 100px auto 0;
}
table {
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #c0c0c0;
width: 500px;
}
th,
td {
border: 1px solid #d0d0d0;
color: #404060;
padding: 10px;
}
th {
background-color: #09c;
font: bold 16px "微软雅黑";
color: #fff;
}
td {
font: 14px "微软雅黑";
}
tbody tr {
background-color: #f0f0f0;
cursor: pointer;
}
.current {
background-color: red!important;
}
</style>
</head>
<body>
<div class="wrap">
<table>
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>课程</th>
<th>成绩</th>
</tr>
</thead>
<tbody id="target">
<tr>
<td>
1
</td>
<td>吕不韦</td>
<td>语文</td>
<td>100</td>
</tr>
<tr>
<td>
2
</td>
<td>吕布</td>
<td>日语</td>
<td>100</td>
</tr>
<tr>
<td>
3
</td>
<td>吕蒙</td>
<td>营销学</td>
<td>100</td>
</tr>
<tr>
<td>
4
</td>
<td>吕尚</td>
<td>数学</td>
<td>100</td>
</tr>
<tr>
<td>
5
</td>
<td>吕雉</td>
<td>英语</td>
<td>100</td>
</tr>
<tr>
<td>
6
</td>
<td>吕超</td>
<td>体育</td>
<td>100</td>
</tr>
</tbody>
</table>
</div>
<script>
//需求:让tr各行变色,鼠标放入tr中,高亮显示。
//1.隔行变色。
var tbody = document.getElementById("target");
var trArr = tbody.children;
//循环判断并各行赋值属性(背景色)
for(var i=0;i<trArr.length;i++){
if(i%2!==0){
trArr[i].style.backgroundColor = "#a3a3a3";
}else{
trArr[i].style.backgroundColor = "#ccc";ß
}
//鼠标进入高亮显示
//难点:鼠标移开的时候要回复原始颜色。
//计数器(进入tr之后,立刻记录颜色,然后移开的时候使用记录好的颜色)
var color = "";
trArr[i].onmouseover = function () {
//赋值颜色之前,先记录颜色
color = this.style.backgroundColor;
this.style.backgroundColor = "#fff";
}
trArr[i].onmouseout = function () {
this.style.backgroundColor = color;
}
}
</script>
</body>
</html> 高级隔行变色<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
body {
background: url(image/1.jpg) no-repeat;
}
.box {
height: 165px;
padding-top: 35px;
text-align: center;
background: rgba(255,255,255,0.3);
}
img {
cursor: pointer;
margin: 0 10px;
}
</style>
</head>
<body>
<div class="box">
<img src="image/1.jpg" alt="" width="200px"/>
<img src="image/2.jpg" alt="" width="200px"/>
<img src="image/3.jpg" alt="" width="200px"/>
<img src="image/4.jpg" alt="" width="200px"/>
<img src="image/5.jpg" alt="" width="200px"/>
</div>
<script>
//需求:点击图片,body的背景图修改。
//步骤:
//1.获取事件源
//2.绑定事件
//3.书写事件驱动程序
//1.获取事件源
var box = document.getElementsByTagName("div")[0];
//body的获取js内部帮我们优化完毕
var body = document.body;
var imgArr = box.children;
//2.绑定事件
for(var i=0;i<imgArr.length;i++){
imgArr[i].index = i;
imgArr[i].onclick = function () {
//3.书写事件驱动程序
//改body的背景
// body.style.backgroundImage = "url(image/"+(this.index+1)+".jpg)";
body.style.backgroundImage = "url("+this.src+")";
}
}
</script>
</body>
</html> 百度皮肤<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
body {
background: url(image/1.jpg) no-repeat;
}
.box {
height: 165px;
padding-top: 35px;
text-align: center;
background: rgba(255,255,255,0.3);
}
img {
cursor: pointer;
margin: 0 10px;
}
</style>
</head>
<body>
<div class="box">
<img src="image/1.jpg" alt="" width="200px"/>
<img src="image/2.jpg" alt="" width="200px"/>
<img src="image/3.jpg" alt="" width="200px"/>
<img src="image/4.jpg" alt="" width="200px"/>
<img src="image/5.jpg" alt="" width="200px"/>
</div>
<script>
//需求:点击图片,body的背景图修改。
//步骤:
//1.获取事件源
//2.绑定事件
//3.书写事件驱动程序
//1.获取事件源
var box = document.getElementsByTagName("div")[0];
//body的获取js内部帮我们优化完毕
var body = document.body;
var imgArr = box.children;
//2.绑定事件
for(var i=0;i<imgArr.length;i++){
imgArr[i].index = i;
imgArr[i].onclick = function () {
//3.书写事件驱动程序
//改body的背景
// body.style.backgroundImage = "url(image/"+(this.index+1)+".jpg)";
body.style.backgroundImage = "url("+this.src+")";
}
}
</script>
</body>
</html> 隐藏盒子的方式<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {
margin-top: 20px;
width: 200px;
height: 200px;
background-color: red;
position: absolute;
top: 100px;
left: 10px;
z-index: 1;
}
img {
position: absolute;
top: 100px;
left: 10px;
}
</style>
</head>
<body>
<button>进化吧,皮卡丘!!!</button>
<div></div>
<img src="image/mm.png" alt="" width="200px"/>
<script>
//需求:点击按钮,让盒子和图片同事移开,并且,图片压住盒子.
var btn = document.getElementsByTagName("button")[0];
var div = document.getElementsByTagName("div")[0];
var img = document.getElementsByTagName("img")[0];
//绑定事件
btn.onclick = function () {
div.style.top = "300px";
div.style.left = "300px";
img.style.top = "300px";
img.style.left = "300px";
img.style.zIndex = "2";
}
</script>
</body>
</html> 定位和层级dom元素的创建方法
1.documrnt.write(“<li></li>”)
A) script写在那就创建在那
B) 会被覆盖
2.parentNode.innerHTML += “<li></li>”
3.已有节点.appendChild(创建的节点);追加新节点到已有节点的最后面
var ul = document.getElementsByTagName('ul')[0]; //获取已有元素 var li = document.createElement('li'); //创建一个li标签 var liText = document.createTextNode('我是一个LI标签'); //创建一个文本节点 ul.appendChild(li); //把li标签追加到ul最后面 li.appendChild(liText); //把文本节点追加到li标签中
4.已有节点.insertBefore(创建的节点,已有节点的子节点节点);在已有节点的子节点之前创建一个新的节点
var ul = document.getElementsByTagName('ul')[0]; //获取已有元素 var li = document.createElement('li'); //创建一个li标签 var liText = document.createTextNode('我是一个LI标签'); //创建一个文本节点 li.appendChild(liText); //把文本节点追加到li标签中 var list = document.getElementsByTagName('li')[2]; //获取ul的一个子节点 ul.insertBefore(li,list); //添加在list标签之前
5.删除 父节点.removeChild(要删除的子节点);
A) var ul = document.getElementsByTagName('ul')[0]; //获取已有元素 var li = document.createElement('li'); //创建一个li标签 var liText = document.createTextNode('我是一个LI标签'); //创建一个文本节点 var list = document.getElementsByTagName('li')[2]; //获取ul的一个子节点 ul.removeChild(list); //删除ul里的list节点
list.parentNode.removeChild(list); //现照到list的父元素,在进行删除,可以不定义父元素
6.替换 父节点.replaceChild(新节点,老节点);
A) var ul = document.getElementsByTagName('ul')[0]; //创建一个文本节点 var li = document.createElement('li'); //创建一个li标签 var liText = document.createTextNode('我是一个LI标签'); //创建一个文本节点 li.appendChild(liText); //把文本节点追加到li标签中 var list = document.getElementsByTagName('li')[2]; //replaceChild 替换节点 ul.replaceChild(liText,list); //liText替换叼list
7.克隆 var 变量 = 被克隆的节点.cloneNode(true); (如果true改为false,那么将克隆一个空节点)
var newNode = li.cloneNode(true); //克隆一个li节点给变量newNode ul.appendChild (newNode); //将克隆的节点添加到ul之后
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button>创建</button>
<ul>
aaaaa
</ul>
<script>
//第一种创建方式:document.write();
document.write("<li>我是document.write创建的</li>");
var btn = document.getElementsByTagName("button")[0];
var ul = document.getElementsByTagName("ul")[0];
// btn.onclick = function () {
// document.write("<li>我是document.write创建的</li>");
// }
//第二种:直接利用元素的innerHTNL方法。(innerText方法不识别标签)
ul.innerHTML += "<li id='li1'>我是innerHTML创建的</li>";
//第三种:利用dom的api创建元素
var newLi = document.createElement("li");
newLi.innerHTML = "我是createElement创建的";
// console.log(newLi);
//在父元素的最后面添加元素。
// ul.appendChild(newLi);
//指定位置添加
var li1 = document.getElementById("li1");
ul.insertBefore(newLi,li1);
</script>
</body>
</html> dom元素的创建<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul>
<li id="li1">我是参照标签</li>
</ul>
<script>
//document.write();不常用,因为容易覆盖原来的页面。
//innerHTML;用的比较多。绑定属性和内容比较方便。(节点套节点)
//document.createElement;也是比较多的,指定数量的时候一般用它。
var ul = document.getElementsByTagName("ul")[0];
var li1 = document.getElementById("li1");
//创建,添加,删除,替换
var li2 = document.createElement("li");
li2.innerText = "我是createElement创建的标签,用的是appendChild的方法添加的";
ul.appendChild(li2);
//用insertBefore添加
var li3 = document.createElement("li");
li3.innerText = "我是createElement创建的标签,用的是insertBefore的方法添加的"
//父节点.insertBefore(新节点,参照节点);
ul.insertBefore(li3,li1);
//删除,替换
ul.removeChild(li3);
//父节点.replaceChild(newNode,oldNode);
ul.replaceChild(li3,li2);
//克隆node.cloneNode(true);深层复制。
for(var i=0;i<=3;i++){
var newLi = li3.cloneNode(true);
ul.appendChild(newLi);
}
</script>
</body>
</html> 元素的操作<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
li {
cursor: pointer;
}
</style>
</head>
<body>
<button>创建列表</button>
<ul></ul>
<script>
//需求:点击列表,添加四大名人,然后鼠标放到谁上面,谁高亮显示。
//思路:创建一个数组,添加内容。利用for循环穿件li标签在添加内容,高亮显示。
//步骤:(指定多少个元素的创建最好用for循环)
//老三步
var btn = document.getElementsByTagName("button")[0];
//获取相关元素并定义内容
var arr = ["黑崎一护","乌尔奇奥拉","冰轮丸","名侦探柯南"];
var ul = document.getElementsByTagName("ul")[0];
btn.onclick = function () {
// ul.innerHTML = "";
for(var i=0;i<arr.length;i++){
//创建li元素
var newLi = document.createElement("li");
newLi.innerHTML += "<li>"+arr[i]+"</li>";
//添加到ul中
ul.appendChild(newLi);
}
//获取所有的li标签然后,为他绑定事件,排他思想,高亮显示
var liArr = ul.children;
//为所有的li绑定事件
for(var i=0;i<liArr.length;i++){
liArr[i].onmouseover = function () {
//排他思想,高亮显示
for(var j=0;j<liArr.length;j++){
liArr[j].style.backgroundColor = "";
}
this.style.backgroundColor = "red";
}
}
}
</script>
</body>
</html> 创建列表,高亮显示<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body {
margin: 0 auto;
padding: 0px;
font-size: 12px;
background: url(image/bg.gif) repeat center 36px;
text-align: center;
background-color: #c30230;
}
#content {
margin: 0 auto;
width: 960px;
background: url(image/content_bg.jpg) no-repeat left top;
height: 627px;
position: relative;
}
#content .tip1, #content .tip2, #content .tip3, #content .tip4, #content .tip5, #content .tip6, #content .tip7, #content .tip8 {
position: absolute;
width: 227px;
left: 200px;
top: 100px;
}
#content .tip1 .tip_h {
background: url(image/tip1_h.gif) no-repeat left top;
}
#content .tip1 .tip_h, #content .tip2 .tip_h, #content .tip3 .tip_h, #content .tip4 .tip_h, #content .tip5 .tip_h, #content .tip6 .tip_h, #content .tip7 .tip_h, #content .tip8 .tip_h {
width: 227px;
padding-top: 45px;
height: 23px;
text-align: left;
cursor: move;
}
#content .tip1 .tip_c {
background: url(image/tip1_c.gif) repeat-y;
}
#content .tip1 .tip_c, #content .tip2 .tip_c, #content .tip3 .tip_c, #content .tip4 .tip_c, #content .tip5 .tip_c, #content .tip6 .tip_c, #content .tip7 .tip_c, #content .tip8 .tip_c {
width: 200px;
padding-left: 12px;
padding-right: 15px;
min-height: 40px;
text-align: left;
line-height: 20px;
max-height: 160px;
word-wrap: break-word;
word-break: break-all;
overflow: hidden;
}
#content .tip1 .tip_f {
background: url(image/tip1_f.gif) no-repeat left top;
}
#content .tip1 .tip_f, #content .tip2 .tip_f, #content .tip3 .tip_f, #content .tip4 .tip_f, #content .tip5 .tip_f, #content .tip6 .tip_f, #content .tip7 .tip_f, #content .tip8 .tip_f {
width: 227px;
height: 53px;
padding-top: 20px;
}
#content .close, #content .close2 {
float: left;
font-size: 12px;
cursor: pointer;
color: #000000;
}
.clr {
clear: both;
overflow: auto;
display: block;
height: 0px;
}
#content .icon {
float: left;
width: 35px;
padding-left: 15px;
height: 35px;
text-align: center;
}
#content .name {
float: right;
padding-right: 15px;
text-align: right;
font-size: 14px;
line-height: 35px;
color: #C0F;
}
#content .num {
float: left;
padding-left: 7px;
width: 195px;
}
</style>
</head>
<body>
<!--纸条墙-->
<div id="content">
<!--纸条-->
<div class="tip1" id="tip" >
<div class="tip_h" title="双击关闭纸条">
<div class="num">第[49568]条 2016-07-7 22:51:52</div>
<div class="close" title="关闭纸条" >×</div>
<div class="clr"></div>
</div>
<div class="tip_c">
普天同庆,天下大同!
</div>
<div class="tip_f">
<div class="icon">
<img src="image/bpic_1.gif" alt="" title="">
</div>
<div class="name">不愿意透露姓名的吕先生</div>
<div class="clr"></div>
</div>
</div>
</div>
<script>
//需求:克隆盒子,然后随机位置,点击纸条的时候压住其他的纸条。
//思路:获取纸条,克隆,随机数值绑定到纸条上,添加到父盒子中,然后绑定事件层级最高。
//步骤:
//1.获取纸条,克隆
//2.随机数值绑定到纸条上
//3.添加到父盒子中
//4.然后绑定事件层级最高
//0.创建多张纸条
for(var i=1;i<=10;i++){
//1.获取纸条,克隆
var tip = document.getElementById("tip");
var content = document.getElementById("content");
var newTip = tip.cloneNode(true);
//问题:id问题。
newTip.id = newTip.id+i;
//2.随机数值绑定到纸条上(top不要超过400,left不要超过700)
var topValue = parseInt(Math.random()*400);//(取值是0-1,那么乘以400,取值就是0-400)
var leftValue = parseInt(Math.random()*700);//小数取整
newTip.style.top = topValue+"px";
newTip.style.left = leftValue+"px";
//3.添加到父盒子中
content.appendChild(newTip);
//4.然后绑定事件层级最高
newTip.onclick = fn;
}
//tip本身有问题,没有绑定事件。(1.删除。 2.在绑定一次)
tip.onclick = fn;
// tip.parentNode.removeChild(tip);
//获取所有类名叫做的tip1标签,然后绑定事件。
var index = 1;
function fn() {
//计数器
this.style.zIndex = index;
index++;
}
// 点击关闭 隐藏盒子
var close = document.getElementsByClassName("close");
for (i=0;i<close.length;i++) {
close[i].onclick = function () {
this.parentNode.parentNode.style.display = "none";
}
}
</script>
</body>
</html> 祝愿墙<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
select {
width: 180px;
height: 240px;
font-size: 16px;
background-color: #7dff51;
}
</style>
<script>
window.onload = function () {
//需求:点击>>>左边的所有选项全部进入右边,
//点击<<<右边的所有选项全部进入左边。
//思路:获取所有的子元素,然后直接添加到另一侧。
//获取相关元素
var sel1 = document.getElementById("sel1");
var sel2 = document.getElementById("sel2");
var btnArr = document.getElementsByTagName("button");
//绑定事件
btnArr[0].onclick = function () {
//获取左侧的select标签的所有子元素
var arr = sel1.children;
//遍历添加到右侧(随着左侧标签内元素的减少,数组的长度也会减少,那么)
for(var i=arr.length-1;i>=0;i--){
//push()是操作数组的。要往一个标签中添加元素,要用appendChild();
//而往另外一个标签中添加元素原来的数组长度容易变化,为了防止跳出循环,我们使用反向遍历数组
sel2.appendChild(arr[arr.length-1-i]);
}
}
btnArr[1].onclick = function () {
//获取左侧的select标签的所有子元素
var arr = sel2.children;
//遍历添加到右侧(随着左侧标签内元素的减少,数组的长度也会减少,那么)
for(var i=arr.length-1;i>=0;i--){
//push()是操作数组的。要往一个标签中添加元素,要用appendChild();
//而往另外一个标签中添加元素原来的数组长度容易变化,为了防止跳出循环,我们使用反向遍历数组
sel1.appendChild(arr[arr.length-1-i]);
}
}
btnArr[2].onclick = function () {
var arr = sel1.children;
sel2.appendChild(arr[arr.length-1]);
};
btnArr[3].onclick = function () {
var arr = sel2.children;
sel1.appendChild(arr[arr.length-1]);
}
}
</script>
</head>
<body>
<!-- multiple -->
<select name="" size="10" id="sel1" multiple>
<option value="">香蕉</option>
<option value="">苹果</option>
<option value="">鸭梨</option>
<option value="">葡萄</option>
</select>
<button>>>></button>
<button><<<</button>
<button>></button>
<button><</button>
<select name="" size="10" id="sel2" multiple>
</select>
</body>
</html> 选择水果