1. 应用JavaScript编写留言的功能,在文本中输入文字提交后,在下方进行显示。
提示:可将下方内容以列表体现,提交时动态创建列表的项。可使用以下两种方式之一的方法:
1) 使用CreateElenment动态创建li标签及li中的文本
2) 在列表标签ul或者ol对象上设置InnerHtml
列表对象.innerHTML += “
- 文本内容
- ”
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <style> .div_main{ width: 254px; } .text{ width: 250px; height: 100px; word-wrap: break-word; } .div2{ float:right; } .div_message{ height: 500px; width: 254px; margin-top:30px; } ul{ margin-top: 20px; padding: 0; } li{ padding-left: 15px; padding-right: 15px; width: 224px; border-bottom: 1px solid; overflow: hidden; margin-top: 20px; margin-bottom: 20px; font:15px 黑体; } </style> <body> <div class="div_main"> <div > <input class="text" name="" id="inputbox"> </div> <div class="div2"> <input type="button" value="提交" id="b1"> </div> <div class="div_message"> <ul id="my_list"> <li> 西南石油大学 </li> </ul> </div> </div> </body> <script type="text/javascript"> var bton=document.getElementById("b1"); bton.onclick=function(){ var UL=document.getElementById("my_list"); var message=document.getElementById("inputbox"); var LI=document.createElement("li"); LI.innerHTML=message.value; document.getElementById("my_list").insertBefore(LI,UL.childNodes[0]) message.value=""; } </script> </html>
2.设计一个选项卡功能,当鼠标移动至某一选项卡时出现切换。
提示:- 选项卡可采用三个行内元素,为选中背景色#000,选中背景色#aaa
- 选项卡内容可采用三个不同列表
- 针对选项卡和选项内容定义两组不同样式。选项卡未选中背景色#000,选中背景色#aaa;文本颜色#fff;;选项内容未选中不显示display:none,选中显示display:block。
- 通过JavaScript动态设置样式,页面对象.classname=“class样式选择器”
如mydiv. className = “selectSpanStyle” - 鼠标移至选项卡设置onmouseenter事件
如:mySpan.onmouseenter = function(){
…………
}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <style> ul{ margin: 0; padding: 0; list-style-type: none; overflow: hidden; width: 450px; height: 30px; } li{ float: left; background-color:#000; width: 150px; height: 30px; color:#FFF; font: 18px 黑体; text-align: center; } li:hover{ background-color: #aaa; } section{ height: 300px; } body{ background-color:lightgrey; width:450px; } .div_main{ margin-left: 100px; height: 30px; width: 450px; } .div_news{ background-color:#FFF; border-bottom-color:#aaa; border-bottom-style: dotted; border-bottom-width: 1px; padding-left: 10px; padding-bottom: 5px; padding-top: 5px; } </style> <body> <nav id="nav"> <ul> <li class="act">热图排行</li> <li>美图速递</li> <li>前沿科技</li> </ul> </nav> <div id="container"> <section class="tab" > <div class="div_news">aaaaaaaaa</div> <div class="div_news">bbbbbbbbbbb</div> <div class="div_news">ccccccccccccccc</div> <div class="div_news">dddddddddddddd</div> </section> <section class="tab" > <div class="div_news">测试1</div> <div class="div_news">测试2</div> <div class="div_news">测试3</div> <div class="div_news">测试4</div> </section> <section class="tab" > <div class="div_news">测试1测试1测试1测试1</div> <div class="div_news">测试2测试2测试2</div> <div class="div_news">测试3测试3测试3</div> <div class="div_news">测试4测试4测试4</div> </section> </div> </body> <script> window.onload=function () { var nav=document.getElementById('nav'); var oNav=nav.getElementsByTagName('li'); var container=document.getElementById('container'); var oDiv=container.getElementsByClassName('tab'); for(var i=0;i<oNav.length;i++){ oNav[i].index=i; oNav[i].onmouseenter =function () { for(var i=0;i<oNav.length;i++){ oNav[i].className=''; oDiv[i].style.display="none"; } this.className='act'; oDiv[this.index].style.display="block" } for(var m=1;m<oNav.length;m++){ oNav[m].className=''; oDiv[m].style.display="none"; } } }; </script> </html>
3.设计如下表单,并进行数据验证。
提示:- 输入元素取值可通过var name =document.getElementsById(“元素id”).value;
- 判断长度name.length
- 是否英文字符开头
- 首字母是大小写字符,获取输入的字符对应的编码
var keycode=name.charCodeAt(0);
if(keycode <65||( keycode >90&& keycode <97)|| keycode >122){
不是英文字符
}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <style> body{ background-color: darkgray; height:90%;width:40%; position:fixed; top:0;right:0;bottom:0;left:0; margin:auto; } .div_bigbox{ width: 450px; } .div_tiltle { padding: 10px; text-align: center; background-color: skyblue; } .div_tiltle a{ text-align: center; color: #fff; font-size: 20px; font-weight: bold; } .div_box2{ background-color: lightblue; height: 300px; padding-left: 70px; padding-top: 30px; } .id{ height: 35px; width: 300px; padding-left: 10px; } .password{ height: 35px; width: 300px; padding-left: 10px; } .warn1{ font-size: 15px; color:red; display:none; } .warn2{ font-size: 15px; color:red; display:block; } .warnbox{ padding-top: 5px; padding-bottom: 5px; margin: 0px; height: 40px; } button{ height: 40px; width: 300px; background-color:darkcyan; color:#fff; font-size: 20px; } </style> <body> <div class="div_bigbox"> <div class="div_tiltle"> <a>注册</a> </div> <div class="div_box2"> <input class="id" id="name" placeholder="请输入您的用户名"> <div class="warnbox"> <p class="warn1" id="w1">用户名长度必须为6到18位! </a> <p class="warn1" id="w2">用户名必须以英文字母开头!</a> </div> <input class="password" id="key" placeholder="请输入您的密码"> <div class="warnbox"> <p class="warn1" id="w3">密码长度不能小于6位!</a> </div> <button id ="btn">提交</button> </div> </div> </body> <script > window.onload=function () { var bton=document.getElementById("btn"); var W1=document.getElementById("w1"); var W2=document.getElementById("w2"); var W3=document.getElementById("w3"); bton.onclick=function(){ W1.setAttribute('style','display:none'); W2.setAttribute('style','display:none'); W3.setAttribute('style','display:none'); var username=document.getElementById("name").value; var userkey=document.getElementById("key").value; if(username.length>18||username.length<6){ W1.setAttribute('style','display:block'); } var keynode=username.charCodeAt(0); if(userkey.length<6){ W3.setAttribute('style','display:block'); } var keycode=username.charCodeAt(0); if(keycode <65||( keycode >90&& keycode <97)|| keycode >122){ W2.setAttribute('style','display:block'); } } } </script> </html>