获取地址栏参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取地址栏参数</title>
<script type="text/javascript">
window.onload = function(){
//url?aa=tom#12
var data = window.location.search;//?aa=tom
var hash = window.location.hash;//#12
alert(hash);//#12
var oSpan = document.getElementById('span01');
// alert(data);//?aa=tom
var arr = data.split('=');
// alert(arr);//?aa,tom
var name = arr[1];
oSpan.innerHTML = name;
}
</script>
</head>
<body>
<div>欢迎<span id="span01"></span>访问我的主页</div>
</body>
</html>
......................................................................................................................................................................
Math
<script type="text/javascript">
// var num = Math.random();
// alert(num);//弹出0-1之间的随机数
var a = 10;
var b = 20;
// var num = Math.random()*(b-a)+a;
// alert(num);//弹出10-20之间的随机数
var arr = [];
for(var i=0; i<20; i++){
// var num = Math.floor(Math.random()*(b-a)+a);//向下取整,10-19
var num = Math.floor(Math.random()*(b-a + 1)+a);//向下取整,10-20
arr.push(num);//生成一个数就放进数组
}
alert(arr);//17,20,20,11,11,19,17,16,10,11,16,11,18,13,13,11,17,14,19,19
</script>
..............................................................................................................................................................
单体创建对象
<script type="text/javascript">
var Tom = {
// 属性
name:'tom',
age:18,
// 方法
showName:function(){
alert(this.name);
},
showAge:function(){
alert(this.age);
}
}
//调用属性
alert(Tom.name);
alert(Tom.age);
//调用方法
Tom.showName();
</script>
..............................................................................................................................................................
工厂模式创建对象
<script type="text/javascript">
function Person(name,age,job){
//创建一个空对象
// var o = new Object();//方式一
var o = {};//方式二
o.name = name;
o.age = age;
o.job = job;
o.showName = function(){
alert(this.name);
}
o.showAge = function(){
alert(this.age);
}
o.showJob = function(){
alert(this.job);
}
return o;
}
var Tom = Person('tom',18,'程序猿');
Tom.showJob();
var Jack = Person('jack',19,'攻城狮');
Jack.showJob();
</script>