java入门1

来源:互联网 发布:嗅探 软件 编辑:程序博客网 时间:2024/05/20 23:07
<!doctype html><html><head><meta charset="utf-8"><title>强制数据类型转换</title><script type="text/javascript">var num=Number('1dfg2');console.info(num+'--------'+typeof(num));var num1=Boolean('1');console.info(num1+'--------'+typeof(num1));var num2=String('12');console.info(num2+'--------'+typeof(num2));//常用的方法//var b=confirm('您确定要删除吗');//警告框//console.info(b);//var str=prompt("提示信息","文文");//console.info(str);var b1=isNaN(12);//判断一个值是否不是一个数字,如果不是数字返回true,否则falseconsole.info(b1);//falsevar num5=parseInt('12a');//将字符串转换为整数console.info(num5);//12var num6=parseFloat('12.1324.456');//将字符串转换为浮点型console.info(num6);//12.1234var num8=parseInt('a12a');//将字符串转换为整数//NaNconsole.info(num8+'****************************************');var num9=eval(1+'2');//计算字符串console.info(num9);//12</script></head><body></body></html>
<!doctype html><html><head><meta charset="utf-8"><title>Object</title><script type="text/javascript">//创建对象var obj=new Object();var str=new String();//如果调用无参数的构造方法var obj2=new Object;//1.创建pervar per=new Object();per.name='wenwen';//给属性赋值per.age=12;console.info(per.name+'-----'+per.age);//2.创建person对象var person={name:'wenwen',age:'22'};//var person={'name':'wenwen','age':'22'};console.info(person);console.info(person.name+'-------'+person.age);//wenwen------22//3创建person3对象var person3=new Object();person3['name']='wen';person3['age']=22;console.info(person3['name']+'------'+person3['age']);//wen--------22 console.info(person3.hasOwnProperty('name'));//true   //判断对象是否有指定的属性//Object.prototype 该对象的对象原型的引用Object.prototype.show=function(){alert('my show time');}person3.show();</script></head><body></body></html>
<!doctype html><html><head><meta charset="utf-8"><title>String</title><script type="text/javascript">//string可用于处理或格式化文本字符串以及确定和定位字符串中的子字符串var str1=new String();console.info(str1);var str2=new String("wenwen");console.info(str2);var str3="hello";console.info(str3);console.info(str3.length);console.info(str2.toString());//转换成字符串var str4=new String('aa:bb:cc');var arr=str4.split(':');//分割console.info(arr.length);
document.write('<br/>'+arr+'<br/>');//aa,bb,ccdocument.write('hello'+str4.sub());//下标var str5=str4.substr(2,3);//start  lengthvar str6=str4.substring(1,4);//start  end(不含)console.info(str5+'-------'+str6);</script></head><body></body></html>
<!doctype html><html><head><meta charset="utf-8"><title>Array</title><script type="text/javascript">//Array:提供对创建任何数据类型的数组的支持。//创建数组对象var arr1=new Array();var arr2=new Array(3);//存放三个元素var arr3=new Array('a','b','v');var arr4=[1,2,3,4];arr2[0]=1;arr2[1]=2;arr2[2]=3;arr2[4]=3;//数组赋值时跳过下标为3,但是此元素存在,undefinedfor(var i=0;i<arr2.length;i++){console.info(arr2[i]);//1 2 3 undefined 3}console.info(arr2.length);//长度为5var arr5=new Array('saaaa','bbb','ccc');console.info(arr5);// ["saaaa", "bbb", "ccc"]document.write(arr5+"<br/>");//saaaa,bbb,ccc//返回字符串值  ,其中包含了连接到一起的数组的所有元素,元素由指定的分隔符分隔开来document.write(arr5.join('|'));//saaaa|bbb|cccdocument.write('<br/>'+arr5.join('*'));//saaaa*bbb*ccc//将新元素添加到一个数组中,并返回数组的新长度值arr5.push('hello');document.write('<br/>'+arr5);//saaaa,bbb,ccc,hello//返回一个元素顺序被反传的Array对象document.write('<br/>'+arr5.reverse());//hello,ccc,bbb,saaaadocument.write('<br/>'+arr5.sort());//bbb,ccc,hello,saaaa 按顺序排序</script></head><body></body></html>
<!doctype html><html><head><meta charset="utf-8"><title>跑马灯</title><script type="text/javascript">//document.title 文档的标题//(window.可以直接干掉)setTimeOut('函数',1000);延迟加载器var str='* * * * 欢迎球球,团子,肉团等家人加入团子家族 * * * *';var arr=str.split('');function run(){//获取第一个元素var first=arr.shift();//移除数组中的第一个元素并返回该元素arr.push(first);document.title=arr.join(" ");setTimeout('run()',100);}run();</script></head><body></body></html>
<!doctype html><html><head><meta charset="utf-8"><title>随机改变颜色</title><style>p{width:200px;height:30px;background:#ccc;color:rgb(255,255,255);}</style><script type="text/javascript">document.getElementById();//根据Id获取标签元素document.getElementById().style.color  //获取文本颜色//获取颜色的方法function color(){var r=Math.floor(Math.random()*256);var g=Math.floor(Math.random()*256);var b=Math.floor(Math.random()*256);return 'rgb('+r+','+g+','+b+')';}function randomText(){document.getElementById('d1').style.color=color();document.getElementById('d2').style.color=color();document.getElementById('d3').style.color=color();setTimeout('randomText()',800);}</script></head><body><p id="d1">湖南工学院</p><p id="d2">湖南工学院</p><p id="d3">湖南工学院</p><script type="text/javascript">randomText();//调用方法</script></body></html>
<!doctype html><html><head><meta charset="utf-8"><title>Math</title><script type="text/javascript">//Math是一个固有对象。提供基本数学函数和常数document.write(Math.E);document.write('<br/>'+Math.PI+'<br/>');document.write('<br/>'+Math.max(12,13)+'<br/>');document.write('<br/>'+Math.random()+'<br/>');document.write('<br/>'+Math.floor(Math.random()*10)+'<br/>');</script></head><body></body></html>
0 0