JavaScript 常用内置对象

123 阅读1分钟

仅供学习,转载请注明出处

1、document

document.getElementById //通过id获取元素
document.getElementsByTagName //通过标签名获取元素
document.referrer  //获取上一个跳转页面的地址(需要服务器环境)

2、location

window.location.href  //获取或者重定url地址
window.location.search //获取地址参数部分
window.location.hash //获取页面锚点或者叫哈希值

练习

通过地址栏的参数改变页面状态

3、Math

Math.random 获取0-1的随机数
Math.floor 向下取整
Math.ceil 向上取整

练习

制作一定范围内的随机整数

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script type="text/javascript">
		// 取 10 ,20 返回的随机数
		window.onload = function(){

			// var iNum = Math.random();
			// alert(iNum); // 生成 0~1的随机数,不会生成1

			function randNum(){
				var rNum = Math.floor((20 - 10) * Math.random() + 10);

				return rNum;
			}

			// alert(randNum());
			var aList = [];
			for(var i = 0; i < 30; i++){
				var iNum = randNum();
				aList.push(iNum);
			}

			console.log(aList);
		}
	</script>
</head>
<body>

</body>
</html>