js问题
来源:互联网 发布:跑腿软件哪个好 编辑:程序博客网 时间:2024/06/05 15:29
//获取今天
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> </style></head><body><script> var weeks =["日","一","二","三","四","五","六"]; var today = new Date(), year = today.getFullYear(), month = today.getMonth()+1, day = today.getDate(), week = today.getDay(), hours = today.getHours(), minutes = today.getMinutes(), seconds = today.getSeconds(), mileseconds =today.getTime();alert("今天是"+year+"年"+month+"月"+day+"日"+"星期"+weeks[week]+hours+"时"+minutes+"分"+seconds+"秒");</script></body></html>
//获取n天后的日期
<!doctype html><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><body><script> function addZero(num){ if(num<10){ return '0'+num; }else{ return num; } } // 返回n天之后的日期时间对象 function get_date(n){ // 判断n,如果是未定义的,则返回当前日期,否则返回n天之后的日期 n=typeof(n)==="undefined"?0:n; // 创建一个当前的日期时间对象 var date=new Date(), times=date.getTime(), // 到现在为止的毫秒数 tempDate=new Date(), // 未来的一个日期对象 //times=date*1; // 等价于getTime()隐式类型转换 tempTimes=times+86400000*n, year,mon,day; // 将tempTimes设置为当前 tempDate.setTime(tempTimes); year=tempDate.getFullYear(); mon=addZero(tempDate.getMonth()+1); day=addZero(tempDate.getDate()); return year+'-'+mon+'-'+day; } console.log(get_date(20));</script></body></html>
//求平均值
function getAvg(){ // 多所有参数进行求和 var sum=0,len=arguments.length,i; for(i=0;i<len;i++){ //sum=sum+arguments[i]; sum+=arguments[i]; // sum=0+5 // sum=0+5+66 // sum=0+5+66+45 } return sum/len; } var avg=getAvg(5,66,45,32,88,24,40,199,3900); console.log(avg);
//设置一个按钮 点击会产生变化
<!doctype html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style> .lock{width:140px;height:30px;line-height: 30px;background:#00f; color:#fff;font-size:14px;text-align:center;border-radius:5px; cursor:pointer;margin-top:30px;} .unlock{width:140px;height:30px;line-height: 30px;background:#666; color:#ccc;font-size:14px;text-align:center;border-radius:5px; cursor:pointer;margin-top:30px;}</style></head><body><div class="lock" id="btn">锁定</div><script> // 获取按钮 var btn=document.getElementById("btn"); function clickBtn(){ alert("我是按钮"); } // 点击按钮调用clickBtn这个函数 btn.onclick=clickBtn;//注意这个写法 没有任何括号 // 给按钮绑定事件,this是对该DOM元素的引用 btn.onclick=function(){ // 判断如果按钮是锁定,则显示为解锁,变为灰色,否则显示为锁定,变为蓝色 if(this.className=="lock"){ this.className="unlock"; this.innerHTML="解锁"; }else{ this.className="lock"; this.innerHTML="锁定"; } //第二种方法 /* if(this.innerHTML=="锁定"){ this.className="unlock"; this.innerHTML="解锁"; }else{ this.className="lock"; this.innerHTML="锁定"; }*/ }</script></body></html>
//判断输入的手机号是否正确
<!doctype html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style> .box{ padding:50px; } .left,.tip{ float:left; } .left{margin-right:10px;} .tip{display:none;font-size:14px;}</style><script> window.onload=function(){ // 获取文本框和提示框 var phone=document.getElementById("phone"), tip=document.getElementById("tip"); // 给文本框绑定激活的事件 phone.onfocus=function(){ // 让tip显示出来 tip.style.display='block'; }; // 给文本框绑定失去焦点的事件 phone.onblur=function(){ // 获取文本框的值,value用于获取表单元素的值 var phoneVal=this.value; // 判断手机号码是否是11位的数字 // 如果输入正确,则显示对号图标,否则显示错号图标 if(phoneVal.length===11 && isNaN(phoneVal)===false){ tip.innerHTML='<img src="img/right.png">'; }else{ tip.innerHTML='<img src="img/error.png">'; } } }</script></head><body><div class="box"><div class="left"><input type="text" id="phone" placeholder="请输入手机号码"></div><div class="tip" id="tip"> 请输入有效的手机号码</div></div></body></html>
//请在index.html文件中,编写arraysSimilar函数,实现判断传入的两个数组是否相似。具体需求:
1. 数组中的成员类型相同,顺序可以不同。例如[1, true] 与 [false, 2]是相似的。
2. 数组的长度一致。
3. 类型的判断范围,需要区分:String, Boolean, Number, undefined, null, 函数,日期, window.
当以上全部满足,则返回"判定结果:通过",否则返回"判定结果:不通过"。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><script> function type(a) { return a === null? '[object Null]' : Object.prototype.toString.apply(a); //如果在ie678中null会显示为object 所以加个判断 } function arraysSimilar(arr1,arr2) { if(!Array.isArray(arr1) || !Array.isArray(arr2)||arr1.length!==arr2.length){ //如果两个参数为数组且两个数组长度相等 return false; } var arr3 = [], arr4 = [], i,j; for ( i in arr1){ //遍历 arr3.push(type(arr1[i])); //将遍历数组1的元素的类型加到数组3中 } for (j in arr2){ arr4.push(type(arr2[j])); } if(arr3.sort().toString()===arr4.sort().toString()){//判断排序后的数组一和数组二的类型是否对应一致 return true; }else { return false; } }</script><script src="test.js"></script></body></html>test.js
var result=function(){ //以下为多组测试数据 var cases=[{ arr1:[1,true,null], arr2:[null,false,100], expect:true },{ arr1:[function(){},100], arr2:[100,{}], expect:false },{ arr1:[null,999], arr2:[{},444], expect:false },{ arr1:[window,1,true,new Date(),"hahaha",(function(){}),undefined], arr2:[undefined,(function(){}),"okokok",new Date(),false,2,window], expect:true },{ arr1:[new Date()], arr2:[{}], expect:false },{ arr1:[window], arr2:[{}], expect:false },{ arr1:[undefined,1], arr2:[null,2], expect:false },{ arr1:[new Object,new Object,new Object], arr2:[{},{},null], expect:false },{ arr1:null, arr2:null, expect:false },{ arr1:[], arr2:undefined, expect:false },{ arr1:"abc", arr2:"cba", expect:false }]; //使用for循环, 通过arraysSimilar函数验证以上数据是否相似,如相似显示“通过”,否则"不通过",所以大家要完成arraysSimilar函数,具体要求,详见任务要求。 for(var i=0;i<cases.length;i++){ if(arraysSimilar(cases[i].arr1,cases[i].arr2)!==cases[i].expect) { document.write("不通过!case"+(i+1)+"不正确!arr1="+JSON.stringify(cases[i].arr1)+", arr2="+JSON.stringify(cases[i].arr2)+" 的判断结果不是"+cases[i].expect); return false; } } return true;}();document.write("判定结果:"+(result?"通过":"不通过"));
阅读全文
0 0
- JS问题
- js问题
- js 问题
- Js问题
- js问题!
- JS问题
- js问题
- js问题!!!
- js问题
- js问题
- js问题
- js问题合集
- js全半角问题
- js刷新页面问题
- js对象问题
- js的document问题
- JS编码问题
- js中Hashtable问题
- 再谈服务熔断、服务降级的差异性
- tmux-2.5.tar.gz 下载
- cfA. Odds and Ends
- 如何安装 Composer
- 学后端,一步一坑,遇坑就跳,跳完再爬---之mysql与mysql可视化工具
- js问题
- EasyMall第八天
- JavaScript关于表单脚本的一些实用技巧
- CodeForces 50 A.Domino piling(水~)
- EasyMall第九天
- 2017年9月1日23:11:33
- HDU 1102 Constructing Roads(prim求最小生成树)
- Ubuntu uninstalling software completely(include purge configuration and files)
- [HDU]1520 Anniversary party