DOM 操作

DOM: 文档对象模型。基于XML 和html的变成接口。提供给文档一个结构化的表示方法。可以改变文档的内容和呈现方式。DOM属于浏览器。不是javascript语言规范里的规定核心内容

 

一、查找元素

 1:直接查找

  •    document.getElementByID  
  •    document.getElementByName
  •    document.getElementByClassName
  •    document.getElementByTagName

 2:间接查找 

  •   parentNode  父节点包含文本
  •   childNodes   所有子节点,包含文本
  •   firstchild  大儿子
  •   lastchild  小儿子
  •   nextSibling   下一个兄弟节点
  •   previousSibling  上一个兄弟节点

     

  • parentElement           // 父节点标签元素
  • children                // 所有子标签
  • firstElementChild       // 第一个子标签元素
  • lastElementChild        // 最后一个子标签元素
  • nextElementtSibling     // 下一个兄弟标签元素
  • previousElementSibling  // 上一个兄弟标签元素

 

3、 操作

    内容

  •     innerText  文本     获取2个标签之间的内容
  •     InnerHtml  HTML内容    获取2个标签之间的内容
  •     value   直接获取值   input、checkbox、radio、password、select、 textarea

    属性

  •    attributes 获取所有的标签属性
  •    setAttribute(key, value)  设置标签属性
  •    getAttribute(key)  获取指定的标签属性

   样式(class操作)

  •     obj.className   获取所有class名称,字符串类型
  •     obj.classList 获取所有cass名称,列表类型
  •     obj.classList.remove(cls)   添加class
  •     obj.classList.add(cls) 删除class

   标签操作

  •   创建标签
  // 方式一

var tag = document.createElement('a')
tag.innerText = "wupeiqi"
tag.className = "c1"
tag.href = "http://www.cnblogs.com/wupeiqi"
 
// 方式二
var tag = "<a class='c1' href='http://www.cnblogs.com/wupeiqi'>wupeiqi</a>"
 
  • 操作标签
  // 方式一
var obj = "<input type='text' />";
xxx.insertAdjacentHTML("beforeEnd",obj);
xxx.insertAdjacentElement('afterBegin',document.createElement('p'))
 
//注意:第一个参数只能是'beforeBegin'、 'afterBegin'、 'beforeEnd'、 'afterEnd'
 
// 方式二
var tag = document.createElement('a')
xxx.appendChild(tag)
xxx.insertBefore(tag,xxx[1])
  •   标签移动和复制
  • //复制
    
    var newh1 = h1.cloneNode(true);   添加true复制内容,包括文本。不填写则只复制标签
    c.appendChild(newH)
    
    
    
    // 移动位置。 交换h 和c的顺序
    var h = document.getElementById("h1");
    var c = document.getElementById("container");
    c.appendChild(h)

     

   

 

复选框操作

  •    <li><input type="checkbox" value = "1"> 篮球</li>
  •    checks = i1.getElementsByTagName("input")  得到一个列表
  •    checks[0].value  得到具体某一复选框对应的数值 。例如value =1
  •    checks[0].checked 查看是否被选中

 

单选框操作

  •   单选框,需要name一致。
  •    value 获取数据
  •    checked 检查是否被选中

  

下拉框操作

  •        <option value="1"> 北京 </option>
  •        value获取数据  value = “33” 设置数据
  •        默认第一个数据
  •         <option value="1" selected="selected"> 北京 </option> 强制指定被选中
  •        xo.selectedIndex 当前选中的索引
  •        xo.selectedIndex = 2 设置选中数据

 

 

 

 

位置操作

 

  •  scrollTop:滚动条举例顶部高度
  • scrollHeight:总文档高度。自身 + padding

  

  •      clientTop:边框高度
  •      clientHeight:当前文档可见范围高度,占据屏幕高度,自身 + padding

 

  •      offsetTop 当前标签距离“顶部”的高度

               当前标签举例“上部”定位标签的(如果上级是relative 等)

  •      offsetHeigh 可见范围高度,自身+padding+border

 

  表单

   document.ByElementById("form").submit()

 

  其他操作

  

console.log                 输出框
alert                       弹出框
confirm                     确认框
  
// URL和刷新
location.href               获取URL
location.href = "url"       重定向
location.reload()           重新加载
  
// 定时器
setInterval                 多次定时器
clearInterval               清除多次定时器
setTimeout                  单次定时器
clearTimeout                清除单次定时器

 

 this

  表示当前正在操作的标签

 

实例

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset='utf-8' >
 5         <title>欢迎blue shit莅临指导&nbsp;&nbsp;</title>
 6         <script type='text/javascript'>
 7             function Go(){
 8                 var content = document.title;
 9                 var firstChar = content.charAt(0)
10                 var sub = content.substring(1,content.length)
11                 document.title = sub + firstChar;
12             }
13             setInterval('Go()',1000);
14         </script>
15     </head>
16     <body>    
17     </body>
18 </html>
19 
20 跑马灯
字符串操作实例

 

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset='utf-8' />
 5         <title></title>
 6         
 7         <style>
 8             .gray{
 9                 color:gray;
10             }
11             .black{
12                 color:black;
13             }
14         </style>
15         <script type="text/javascript">
16             function Enter(){
17                var id= document.getElementById("tip")
18                id.className = 'black';
19                if(id.value=='请输入关键字'||id.value.trim()==''){
20                     id.value = ''
21                }
22             }
23             function Leave(){
24                 var id= document.getElementById("tip")
25                 var val = id.value;
26                 if(val.length==0||id.value.trim()==''){
27                     id.value = '请输入关键字'
28                     id.className = 'gray';
29                 }else{
30                     id.className = 'black';
31                 }
32             }
33         </script>
34     </head>
35     <body>
36         <input type='text' class='gray' id='tip' value='请输入关键字' onfocus='Enter();'  onblur='Leave();'/>
37     </body>
38 </html>
39 
40 搜索框
搜索框实例--javascript函数使用

 

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8     <input type="button" value="全选"  onclick="CheckAll();"/>
 9     <input type="button" value="取消" onclick="CancelAll();"/>
10     <input type="button" value="反选" onclick="ReverseCheck();"/>
11 
12     <table border="1" >
13         <thead>
14 
15         </thead>
16         <tbody id="tb">
17             <tr>
18                 <td><input type="checkbox" /></td>
19                 <td>111</td>
20                 <td>222</td>
21             </tr>
22             <tr>
23                 <td><input type="checkbox" /></td>
24                 <td>111</td>
25                 <td>222</td>
26             </tr>
27             <tr>
28                 <td><input type="checkbox" /></td>
29                 <td>111</td>
30                 <td>222</td>
31             </tr>
32             <tr>
33                 <td><input type="checkbox" /></td>
34                 <td>111</td>
35                 <td>222</td>
36             </tr>
37         </tbody>
38     </table>
39     <script>
40         function CheckAll(ths){
41             var tb = document.getElementById('tb');
42             var trs = tb.childNodes;
43             for(var i =0; i<trs.length; i++){
44 
45                 var current_tr = trs[i];
46                 if(current_tr.nodeType==1){
47                     var inp = current_tr.firstElementChild.getElementsByTagName('input')[0];
48                     inp.checked = true;
49                 }
50             }
51         }
52 
53         function CancelAll(ths){
54             var tb = document.getElementById('tb');
55             var trs = tb.childNodes;
56             for(var i =0; i<trs.length; i++){
57 
58                 var current_tr = trs[i];
59                 if(current_tr.nodeType==1){
60                     var inp = current_tr.firstElementChild.getElementsByTagName('input')[0];
61                     inp.checked = false;
62                 }
63             }
64         }
65 
66         function ReverseCheck(ths){
67             var tb = document.getElementById('tb');
68             var trs = tb.childNodes;
69             for(var i =0; i<trs.length; i++){
70                 var current_tr = trs[i];
71                 if(current_tr.nodeType==1){
72                     var inp = current_tr.firstElementChild.getElementsByTagName('input')[0];
73                     if(inp.checked){
74                         inp.checked = false;
75                     }else{
76                         inp.checked = true;
77                     }
78                 }
79             }
80         }
81 
82     </script>
83 </body>
84 </html>
85 
86 Demo
全选、取消、反选--复选框操作

 

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body style="margin: 0;">
 8     <div style="height: 900px;">
 9 
10     </div>
11     <div style="padding: 10px;">
12         <div id="i1" style="height:190px;padding: 2px;border: 1px solid red;margin: 8px;">
13                 <p>asdf</p>
14                 <p>asdf</p>
15                 <p>asdf</p>
16                 <p>asdf</p>
17                 <p>asdf</p>
18         </div>
19     </div>
20 
21     <script>
22         var i1 = document.getElementById('i1');
23 
24         console.log(i1.clientHeight); // 可见区域:height + padding
25         console.log(i1.clientTop);    // border高度
26         console.log('=====');
27         console.log(i1.offsetHeight); // 可见区域:height + padding + border
28         console.log(i1.offsetTop);    // 上级定位标签的高度
29         console.log('=====');
30         console.log(i1.scrollHeight);   //全文高:height + padding
31         console.log(i1.scrollTop);      // 滚动高度
32         console.log('=====');
33 
34     </script>
35 </body>
36 </html>
37 
38 test
高度操作

 

 

  1 <!DOCTYPE html>
  2 <html>
  3 <head lang="en">
  4     <meta charset="UTF-8">
  5     <title></title>
  6 </head>
  7 <style>
  8 
  9     body{
 10         margin: 0px;
 11     }
 12     img {
 13         border: 0;
 14     }
 15     ul{
 16         padding: 0;
 17         margin: 0;
 18         list-style: none;
 19     }
 20     .clearfix:after {
 21         content: ".";
 22         display: block;
 23         height: 0;
 24         clear: both;
 25         visibility: hidden;
 26     }
 27 
 28     .wrap{
 29         width: 980px;
 30         margin: 0 auto;
 31     }
 32 
 33     .pg-header{
 34         background-color: #303a40;
 35         -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2);
 36         -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2);
 37         box-shadow: 0 2px 5px rgba(0,0,0,.2);
 38     }
 39     .pg-header .logo{
 40         float: left;
 41         padding:5px 10px 5px 0px;
 42     }
 43     .pg-header .logo img{
 44         vertical-align: middle;
 45         width: 110px;
 46         height: 40px;
 47 
 48     }
 49     .pg-header .nav{
 50         line-height: 50px;
 51     }
 52     .pg-header .nav ul li{
 53         float: left;
 54     }
 55     .pg-header .nav ul li a{
 56         display: block;
 57         color: #ccc;
 58         padding: 0 20px;
 59         text-decoration: none;
 60         font-size: 14px;
 61     }
 62     .pg-header .nav ul li a:hover{
 63         color: #fff;
 64         background-color: #425a66;
 65     }
 66     .pg-body{
 67 
 68     }
 69     .pg-body .catalog{
 70         position: absolute;
 71         top:60px;
 72         width: 200px;
 73         background-color: #fafafa;
 74         bottom: 0px;
 75     }
 76     .pg-body .catalog.fixed{
 77         position: fixed;
 78         top:10px;
 79     }
 80 
 81     .pg-body .catalog .catalog-item.active{
 82         color: #fff;
 83         background-color: #425a66;
 84     }
 85 
 86     .pg-body .content{
 87         position: absolute;
 88         top:60px;
 89         width: 700px;
 90         margin-left: 210px;
 91         background-color: #fafafa;
 92         overflow: auto;
 93     }
 94     .pg-body .content .section{
 95         height: 500px;
 96     }
 97 </style>
 98 <body onscroll="ScrollEvent();">
 99 <div class="pg-header">
100     <div class="wrap clearfix">
101         <div class="logo">
102             <a href="#">
103                 <img src="http://core.pc.lietou-static.com/revs/images/common/logo_7012c4a4.pn">
104             </a>
105         </div>
106         <div class="nav">
107             <ul>
108                 <li>
109                     <a  href="#">首页</a>
110                 </li>
111                 <li>
112                     <a  href="#">功能一</a>
113                 </li>
114                 <li>
115                     <a  href="#">功能二</a>
116                 </li>
117             </ul>
118         </div>
119 
120     </div>
121 </div>
122 <div class="pg-body">
123     <div class="wrap">
124         <div class="catalog">
125             <div class="catalog-item" auto-to="function1"><a>第1张</a></div>
126             <div class="catalog-item" auto-to="function2"><a>第2张</a></div>
127             <div class="catalog-item" auto-to="function3"><a>第3张</a></div>
128         </div>
129         <div class="content">
130             <div menu="function1" class="section">
131                 <h1>第一章</h1>
132             </div>
133             <div menu="function2" class="section">
134                 <h1>第二章</h1>
135             </div>
136             <div menu="function3" class="section">
137                 <h1>第三章</h1>
138             </div>
139         </div>
140     </div>
141 
142 </div>
143     <script>
144         function ScrollEvent(){
145             var bodyScrollTop = document.body.scrollTop;
146             if(bodyScrollTop>50){
147                 document.getElementsByClassName('catalog')[0].classList.add('fixed');
148             }else{
149                 document.getElementsByClassName('catalog')[0].classList.remove('fixed');
150             }
151 
152         }
153     </script>
154 </body>
155 </html>
滚动菜单-固定左侧菜单
  1 <!DOCTYPE html>
  2 <html>
  3 <head lang="en">
  4     <meta charset="UTF-8">
  5     <title></title>
  6 </head>
  7 <style>
  8 
  9     body{
 10         margin: 0px;
 11     }
 12     img {
 13         border: 0;
 14     }
 15     ul{
 16         padding: 0;
 17         margin: 0;
 18         list-style: none;
 19     }
 20     h1{
 21         padding: 0;
 22         margin: 0;
 23     }
 24     .clearfix:after {
 25         content: ".";
 26         display: block;
 27         height: 0;
 28         clear: both;
 29         visibility: hidden;
 30     }
 31 
 32     .wrap{
 33         width: 980px;
 34         margin: 0 auto;
 35     }
 36 
 37     .pg-header{
 38         background-color: #303a40;
 39         -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2);
 40         -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2);
 41         box-shadow: 0 2px 5px rgba(0,0,0,.2);
 42     }
 43     .pg-header .logo{
 44         float: left;
 45         padding:5px 10px 5px 0px;
 46     }
 47     .pg-header .logo img{
 48         vertical-align: middle;
 49         width: 110px;
 50         height: 40px;
 51 
 52     }
 53     .pg-header .nav{
 54         line-height: 50px;
 55     }
 56     .pg-header .nav ul li{
 57         float: left;
 58     }
 59     .pg-header .nav ul li a{
 60         display: block;
 61         color: #ccc;
 62         padding: 0 20px;
 63         text-decoration: none;
 64         font-size: 14px;
 65     }
 66     .pg-header .nav ul li a:hover{
 67         color: #fff;
 68         background-color: #425a66;
 69     }
 70     .pg-body{
 71 
 72     }
 73     .pg-body .catalog{
 74         position: absolute;
 75         top:60px;
 76         width: 200px;
 77         background-color: #fafafa;
 78         bottom: 0px;
 79     }
 80     .pg-body .catalog.fixed{
 81         position: fixed;
 82         top:10px;
 83     }
 84 
 85     .pg-body .catalog .catalog-item.active{
 86         color: #fff;
 87         background-color: #425a66;
 88     }
 89 
 90     .pg-body .content{
 91         position: absolute;
 92         top:60px;
 93         width: 700px;
 94         margin-left: 210px;
 95         background-color: #fafafa;
 96         overflow: auto;
 97     }
 98     .pg-body .content .section{
 99         height: 500px;
100         border: 1px solid red;
101     }
102 </style>
103 <body onscroll="ScrollEvent();">
104 <div class="pg-header">
105     <div class="wrap clearfix">
106         <div class="logo">
107             <a href="#">
108                 <img src="http://core.pc.lietou-static.com/revs/images/common/logo_7012c4a4.pn">
109             </a>
110         </div>
111         <div class="nav">
112             <ul>
113                 <li>
114                     <a  href="#">首页</a>
115                 </li>
116                 <li>
117                     <a  href="#">功能一</a>
118                 </li>
119                 <li>
120                     <a  href="#">功能二</a>
121                 </li>
122             </ul>
123         </div>
124 
125     </div>
126 </div>
127 <div class="pg-body">
128     <div class="wrap">
129         <div class="catalog" id="catalog">
130             <div class="catalog-item" auto-to="function1"><a>第1张</a></div>
131             <div class="catalog-item" auto-to="function2"><a>第2张</a></div>
132             <div class="catalog-item" auto-to="function3"><a>第3张</a></div>
133         </div>
134         <div class="content" id="content">
135             <div menu="function1" class="section">
136                 <h1>第一章</h1>
137             </div>
138             <div menu="function2" class="section">
139                 <h1>第二章</h1>
140             </div>
141             <div menu="function3" class="section">
142                 <h1>第三章</h1>
143             </div>
144         </div>
145     </div>
146 
147 </div>
148     <script>
149         function ScrollEvent(){
150             var bodyScrollTop = document.body.scrollTop;
151             if(bodyScrollTop>50){
152                 document.getElementsByClassName('catalog')[0].classList.add('fixed');
153             }else{
154                 document.getElementsByClassName('catalog')[0].classList.remove('fixed');
155             }
156 
157             var content = document.getElementById('content');
158             var sections = content.children;
159             for(var i=0;i<sections.length;i++){
160                 var current_section = sections[i];
161 
162                 // 当前标签距离顶部绝对高度
163                 var scOffTop = current_section.offsetTop + 60;
164 
165                 // 当前标签距离顶部,相对高度
166                 var offTop = scOffTop - bodyScrollTop;
167 
168                 // 当前标签高度
169                 var height = current_section.scrollHeight;
170 
171                 if(offTop<0 && -offTop < height){
172                     // 当前标签添加active
173                     // 其他移除 active
174                     var menus = document.getElementById('catalog').children;
175                     var current_menu = menus[i];
176                     current_menu.classList.add('active');
177                     for(var j=0;j<menus.length;j++){
178                         if(menus[j] == current_menu){
179 
180                         }else{
181                             menus[j].classList.remove('active');
182                         }
183                     }
184                     break;
185                 }
186 
187             }
188 
189 
190         }
191     </script>
192 </body>
193 </html>
滚动菜单-滚动菜单
  1 <!DOCTYPE html>
  2 <html>
  3 <head lang="en">
  4     <meta charset="UTF-8">
  5     <title></title>
  6 </head>
  7 <style>
  8 
  9     body{
 10         margin: 0px;
 11     }
 12     img {
 13         border: 0;
 14     }
 15     ul{
 16         padding: 0;
 17         margin: 0;
 18         list-style: none;
 19     }
 20     h1{
 21         padding: 0;
 22         margin: 0;
 23     }
 24     .clearfix:after {
 25         content: ".";
 26         display: block;
 27         height: 0;
 28         clear: both;
 29         visibility: hidden;
 30     }
 31 
 32     .wrap{
 33         width: 980px;
 34         margin: 0 auto;
 35     }
 36 
 37     .pg-header{
 38         background-color: #303a40;
 39         -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2);
 40         -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2);
 41         box-shadow: 0 2px 5px rgba(0,0,0,.2);
 42     }
 43     .pg-header .logo{
 44         float: left;
 45         padding:5px 10px 5px 0px;
 46     }
 47     .pg-header .logo img{
 48         vertical-align: middle;
 49         width: 110px;
 50         height: 40px;
 51 
 52     }
 53     .pg-header .nav{
 54         line-height: 50px;
 55     }
 56     .pg-header .nav ul li{
 57         float: left;
 58     }
 59     .pg-header .nav ul li a{
 60         display: block;
 61         color: #ccc;
 62         padding: 0 20px;
 63         text-decoration: none;
 64         font-size: 14px;
 65     }
 66     .pg-header .nav ul li a:hover{
 67         color: #fff;
 68         background-color: #425a66;
 69     }
 70     .pg-body{
 71 
 72     }
 73     .pg-body .catalog{
 74         position: absolute;
 75         top:60px;
 76         width: 200px;
 77         background-color: #fafafa;
 78         bottom: 0px;
 79     }
 80     .pg-body .catalog.fixed{
 81         position: fixed;
 82         top:10px;
 83     }
 84 
 85     .pg-body .catalog .catalog-item.active{
 86         color: #fff;
 87         background-color: #425a66;
 88     }
 89 
 90     .pg-body .content{
 91         position: absolute;
 92         top:60px;
 93         width: 700px;
 94         margin-left: 210px;
 95         background-color: #fafafa;
 96         overflow: auto;
 97     }
 98     .pg-body .content .section{
 99         height: 500px;
100         border: 1px solid red;
101     }
102 </style>
103 <body onscroll="ScrollEvent();">
104 <div class="pg-header">
105     <div class="wrap clearfix">
106         <div class="logo">
107             <a href="#">
108                 <img src="http://core.pc.lietou-static.com/revs/images/common/logo_7012c4a4.pn">
109             </a>
110         </div>
111         <div class="nav">
112             <ul>
113                 <li>
114                     <a  href="#">首页</a>
115                 </li>
116                 <li>
117                     <a  href="#">功能一</a>
118                 </li>
119                 <li>
120                     <a  href="#">功能二</a>
121                 </li>
122             </ul>
123         </div>
124 
125     </div>
126 </div>
127 <div class="pg-body">
128     <div class="wrap">
129         <div class="catalog" id="catalog">
130             <div class="catalog-item" auto-to="function1"><a>第1张</a></div>
131             <div class="catalog-item" auto-to="function2"><a>第2张</a></div>
132             <div class="catalog-item" auto-to="function3"><a>第3张</a></div>
133         </div>
134         <div class="content" id="content">
135             <div menu="function1" class="section">
136                 <h1>第一章</h1>
137             </div>
138             <div menu="function2" class="section">
139                 <h1>第二章</h1>
140             </div>
141             <div menu="function3" class="section" style="height: 200px;">
142                 <h1>第三章</h1>
143             </div>
144         </div>
145     </div>
146 
147 </div>
148     <script>
149         function ScrollEvent(){
150             var bodyScrollTop = document.body.scrollTop;
151             if(bodyScrollTop>50){
152                 document.getElementsByClassName('catalog')[0].classList.add('fixed');
153             }else{
154                 document.getElementsByClassName('catalog')[0].classList.remove('fixed');
155             }
156 
157             var content = document.getElementById('content');
158             var sections = content.children;
159             for(var i=0;i<sections.length;i++){
160                 var current_section = sections[i];
161 
162                 // 当前标签距离顶部绝对高度
163                 var scOffTop = current_section.offsetTop + 60;
164 
165                 // 当前标签距离顶部,相对高度
166                 var offTop = scOffTop - bodyScrollTop;
167 
168                 // 当前标签高度
169                 var height = current_section.scrollHeight;
170 
171                 if(offTop<0 && -offTop < height){
172                     // 当前标签添加active
173                     // 其他移除 active
174 
175                     // 如果已经到底部,现实第三个菜单
176                     // 文档高度 = 滚动高度 + 视口高度
177 
178                     var a = document.getElementsByClassName('content')[0].offsetHeight + 60;
179                     var b = bodyScrollTop + document.documentElement.clientHeight;
180                     console.log(a+60,b);
181                     if(a == b){
182                         var menus = document.getElementById('catalog').children;
183                         var current_menu = document.getElementById('catalog').lastElementChild;
184                         current_menu.classList.add('active');
185                         for(var j=0;j<menus.length;j++){
186                             if(menus[j] == current_menu){
187 
188                             }else{
189                                 menus[j].classList.remove('active');
190                             }
191                         }
192                     }else{
193                         var menus = document.getElementById('catalog').children;
194                         var current_menu = menus[i];
195                         current_menu.classList.add('active');
196                         for(var j=0;j<menus.length;j++){
197                             if(menus[j] == current_menu){
198 
199                             }else{
200                                 menus[j].classList.remove('active');
201                             }
202                         }
203                     }
204 
205 
206 
207 
208                     break;
209                 }
210 
211             }
212 
213 
214         }
215     </script>
216 </body>
217 </html>
滚动菜单-滚动高度

 

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>Title</title>
  6 
  7     <style>
  8        body{
  9             margin:0;
 10             background-color: #dddddd;
 11         }
 12         .pg-header{
 13             background-color:black;
 14             color:white;
 15             height:48px;
 16         }
 17         .w{
 18             margin:0 auto;
 19             width:980px;
 20 
 21         }
 22         .pg-body .menu{
 23             position:absolute;
 24             left:200px;
 25             width:180px;
 26             background-color:white;
 27             float:left;
 28         }
 29         .pg-body .content{
 30             position:absolute;
 31             right:200px;
 32             left:385px;
 33             background-color:white;
 34             float:left;
 35         }
 36 
 37         .pg-body .content .item{
 38             height:500px;
 39         }
 40 
 41         .pg-body .fixed{
 42             position:fixed;
 43             top:10px;
 44 
 45         }
 46         .pg-body .active{
 47             background-color:#2459a2;
 48             color:white;
 49             font-size:18px;
 50         }
 51 
 52     </style>
 53 </head>
 54 <body onscroll="hua();">
 55 <div class="pg-header"></div>
 56 <div class="pg-body">
 57         <div id = "menu"  class="menu">
 58             <ul>
 59                 <li>第一章</li>
 60                 <li>第二章</li>
 61                 <li>第三章</li>
 62             </ul>
 63 
 64         </div>
 65         <div id = "content" class="content">
 66             <div class = "item">第一章</div>
 67             <div class = "item">第二章</div>
 68             <div class = "item">第三章</div>
 69         </div>
 70 
 71 
 72 </div>
 73 
 74 </body>
 75 
 76 <script>
 77 
 78     function  hua(){
 79         var huagao = document.body.scrollTop;
 80         if(huagao >48){
 81             document.getElementById("menu").classList.add("fixed");
 82         }
 83         else{
 84             document.getElementById("menu").classList.remove("fixed");
 85         }
 86 
 87 
 88         var items = document.getElementById("content").children
 89         for(var i=0; i<items.length;i++){
 90             var currentItem = items[i];
 91             currentItem.offsetTop;  //当前标签距离 positin 顶部距离
 92             currentItem.parentElement.offsetTop; //父节点距离顶部距离
 93 
 94             var currentBodyTop =  currentItem.offsetTop +  currentItem.parentElement.offsetTop;
 95 
 96 
 97             var currentWindowTop = currentBodyTop - huagao;
 98 
 99             var currentHeight = currentItem.offsetHeight;
100 
101             var bottomHeight = currentHeight + currentBodyTop;
102             if(currentWindowTop <0 && huagao < bottomHeight){
103                 var ziji = document.getElementsByTagName("li")[i]
104                 ziji.className="active";
105 
106                 var lis = document.getElementsByTagName("li");
107                 for(var j=0;j<lis.length;j++){
108                     if(lis[j]==ziji){}
109                     else{
110                         lis[j].classList.remove("active")
111                     }
112                 }
113 
114             }
115 
116 
117             console.log(document.documentElement.clientHeight);
118             console.log(huagao);
119             console.log(document.body.offsetHeight);
120             console.log(document.getElementById("content").offsetHeight);
121 
122                 if(document.documentElement.clientHeight + huagao == document.body.offsetHeight + document.getElementById("content").offsetHeight){
123                     var liss = document.getElementsByTagName("li");
124                     for(var k=0; k<liss.length;k++){
125                         if(k==liss.length-1){
126                            liss[k].classList.add("active");
127                         }
128                         else{
129                             liss[k].classList.remove("active");
130                         }
131 
132                     }
133                 }
134 
135 
136         }
137     }
138 </script>
139 </html>
练习整体菜单
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 
 9 <ul>
10     <li><input type="radio" name = "sex" value = 11/> 男</li>
11     <li><input type="radio" name = "sex" value = 22/> 女</li>
12     <li><input type="radio"name = "sex"  value = 33/> 中</li>
13 
14 </ul>
15 
16 <script>
17     var radios =  document.getElementsByTagName("radio");
18     radiop[1].value
19 </script>
20 
21 </body>
22 </html>
练习下拉框
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7         .go-top{
 8             position:fixed;
 9             right:28px;
10             bottom:19px;
11             width:40px;
12             background-color:#2459a2;
13             color:white;
14         }
15         .hide{
16             display:none;
17         }
18     </style>
19 </head>
20 <body onscroll="Func();">
21 <div style="height:2000px">
22     <h1>aaaaa</h1>
23 </div>
24 
25 <div id = "i2" class="go-top hide">
26     <a href="javascript:void(0)" onclick = "GoTop();">返回顶部</a>
27 </div>
28 <script>
29 
30     function Func(){
31         var scroll = document.body.scrollTop;
32         var i = document.getElementById("i2")
33         if(scroll>250){
34 
35             i.classList.remove('hide')
36 
37         }
38         else{
39             i.classList.add('hide')
40         }
41     }
42     function GoTop(){
43         document.body.scrollTop=0;
44     }
45 </script>
46 
47 </body>
48 </html>
练习回到顶部
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <div>
 9     <input type="text"/>
10     <input type="button" value="add" onclick="addele(this);"/>
11 </div>
12 <div>
13     <ul id="commentList">
14         <li>alex</li>
15         <li>severn</li>
16     </ul>
17 </div>
18 <script>
19     function addele(ths){
20         var val = ths.previousElementSibling.value;
21 
22         //追加内容
23         var commentlist = document.getElementById("commentList");
24 
25         //第一种追加形式
26 //        var str = "<li>" + val + "</li>";
27 //        commentlist.insertAdjacentHTML("beforeEnd",str);
28 
29         //第二种节点形式针追加
30         var tag = document.createElement('li');
31         tag.innerText = val;
32 //        commentlist.appendChild(tag);
33         commentlist.insertBefore(tag,commentlist.children[1]);
34 
35     }
36 </script>
37 </body>
38 </html>
练习插入标签
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7         ul{
 8             list-style:none;
 9             padding:0;
10             margin:0;
11 
12         }
13         ul li{
14             float:left;
15             padding:8px 10px;
16             background-color:#2459a2;
17             color:white;
18         }
19 
20         .clearfix:after{
21             display:block;
22             content:'.';
23             height:0;
24             visibility:hidden;
25             clear:both;
26         }
27 
28         .tab-menu .title{
29             background-color:#dddddd;
30 
31         }
32         .tab-menu .content{
33             border:1px solid #dddddd;
34             min-height:150px;
35 
36         }
37 
38         .hide{
39             display:none;
40         }
41         .tab-menu .title .active{
42             background-color:white;
43             color:black;
44         }
45     </style>
46 </head>
47 <body>
48 <div style="width:400px; margin:0 auto">
49     <div class="tab-menu">
50         <div class="title clearfix"  >
51             <ul>
52                 <li target = "h1" class = "active" onclick ="show(this);">价格趋势</li>
53                 <li target = "h2" onclick="show(this);">市场分布</li>
54                 <li target = "h3" onclick="show(this);">其他</li>
55             </ul>
56         </div>
57         <div id = "content" class="content">
58             <div con="h1">content1</div>
59             <div con="h2" class = 'hide'>content2</div>
60             <div con="h3" class = 'hide'>content3</div>
61         </div>
62     </div>
63 
64 </div>
65 
66 <script>
67     function show(arg){
68         var target = arg.getAttribute("target");
69         arg.className = "active";
70         var bro = arg.parentElement.children;
71         for(var i=0; i<bro.length;i++){
72             if( arg == bro[i]){
73 
74             }
75             else{
76                 bro[i].removeAttribute("class")
77             }
78         }
79 
80         //操作内容
81 
82         var contents = document.getElementById("content").children;
83         for(var j = 0;j<contents.length;j++){
84             if(contents[j].getAttribute("con")==target){
85                 contents[j].classList.remove("hide");
86 
87             }
88             else{
89                 contents[j].className="hide";
90             }
91         }
92 
93     }
94 </script>
95 </body>
96 </html>
练习tab菜单
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7         .c1{
 8             position:fixed;
 9             top:0;
10             left:0;
11             right:0;
12             bottom:0;
13             background:rgba(0,0,0,0.4);
14             z-index:2;
15         }
16 
17         .c2{
18             position:fixed;
19             top:50%;
20             left:50%;
21             width:400px;
22             height:300px;
23             margin-left:-200px;
24             margin-top:-150px;
25             background-color:white;
26             z-index:3;
27         }
28 
29         .hide{
30             display:none;
31         }
32 
33     </style>
34 </head>
35 <body>
36 
37 <div>
38  <table>
39         <tr>
40             <td>1</td>
41             <td>2</td>
42             <td><input type = "button" value = "click" onclick="show();;"></td>
43         </tr>
44 
45          <tr>
46             <td>1</td>
47             <td>2</td>
48             <td><input type = "button" value = "click" onclick="show();"></td>
49             </tr>
50 
51          <tr>
52             <td>1</td>
53             <td>2</td>
54             <td><input type = "button" value = "click" onclick="show();"></td>
55          </tr>
56 
57     </table>
58 </div>
59 
60 
61 <div id = "shade" class = "c1 hide"></div>
62 
63 <div id = "model" class = "c2 hide">
64 
65     <p>用户:<input type = "text"/></p>
66     <p>密码:<input type = "password"/></p>
67     <p>
68         <input type="button" value = "确定"/>
69         <input type="button" value = "取消" onclick = "hide()"/>
70     </p>
71 </div>
72 
73 <script>
74     function show(){
75         document.getElementById("shade").classList.remove("hide");
76         document.getElementById("model").classList.remove("hide");
77     }
78 
79     function hide(){
80                 document.getElementById("shade").classList.add("hide");
81         document.getElementById("model").classList.add("hide");
82     }
83 </script>
84 
85 </body>
86 </html>
练习模态窗体

 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 
 7     <style>
 8         .gg{
 9             color:gray;
10         }
11         .dd{
12             color:black;
13         }
14     </style>
15 </head>
16 <body>
17 
18 <p>当输入进入,移除内容</p>
19 <p>当鼠标退出时候,添加内容</p>
20 <input type="text" class ="gg" value='请输入内容'  onfocus="Focus(this);"  onblur="Blur(this);"/>
21 
22 
23 <script>
24     function Focus(ths){
25         var currentval = ths.value;
26 
27         if(currentval == "请输入内容"){
28             ths.value = ""
29         }
30 
31         ths.classList.remove("gg");
32         ths.classList.add("dd")
33 
34         console.log(124);
35 
36     }
37 
38     function Blur(ths){
39 
40         var current_val = ths.value;
41         if(current_val == '请输入内容' || current_val.trim().length==0){
42             ths.value = "请输入内容"
43 
44             ths.className='gg'
45 
46         }
47 
48 
49         console.log(13455);
50 
51     }
52 </script>
53 
54 
55 
56 
57 </body>
58 </html>
输入框

 

posted @ 2016-08-13 16:32  咖啡茶  阅读(103)  评论(0)    收藏  举报