1.能打印document中的中的所有属性
window.onload = function (ev) {
for (i in document) {
document.write("document==" + i+"</br>")
}
}
2.定义函数的方法有两种
①使用function声明函数
function f(){ }
②使用Function对象构造函数
var name=new Function(arg1,arg2......,body)
例如:
var say=new Function("name","say","document.write('<h>'+name+say+'</h>')")
say("中","将")
3.将函数作为参数
var a=function (f,x,y) {
return f(x,y)
}
var b=function (x,y) {
return x+y
}
document.write("<h>"+a(b,12,12)+"</h>")
4.使用及时调用函数
(function (x) {
alert(x)
})(12)
-
// 创建空数组,然后在数组末尾操作数组
var a=[] a.push(12,13) for(i in a){ console.log(a[i]) } a.pop() for(i in a){ console.log(a[i]) }
同理对于在数组头部可以使用unshift添加元素,shift退出元素
使用splice(arg1,arg2,arg3) splice:剪接的意思
第一个参数:指定删除的的起始位置
第二个参数:删除的个数
第三个参数:在开始位置插入的数据
var a=[1,2,3,4,5,6,7]
var b=a.splice(2,2,7,8,9)
document.write(a) //1,2,7,8,9,5,6,7
使用
a=a.join("-")
document.write(a) // 1-2-3-4-5-6-7
与join()相反的操作:
a.split("-")
数组反转
a=a.reverse()
数组排序
var sortF = function (x, y) {
return -(x - y)
}
a = a.sort(sortF)
document.write(a)
连接数组
var b=a.concat([12])
document.write(b)
截取子数组
var b=a.slice(2,5) //开始位置和截止位置
document.write(b)
6.BOM(Browser Object Model)浏览器对象模型
window:当body或frameset标签出现时,就会自动创建。
navigator:有关浏览器的信息
screen:
history:
location:包含当前网页文档的url信息
document:整个html文档
①可以直接定义变量,其实默认就存放在window上,作为window的一个属性。
可以用delete window.xxx进行删除这样的属性
但是如果是使用var a="xxx"的形式定义变量,那么就不能直接删除.因为使用var关键字后在window中的configurable会被设置为false
②window定义的人机交互的方法
alert("ee")
var ok=confirm("啦啦啦啦啦") //比第一种多一个取消按钮
console.log(ok)
var ok=prompt("eeee","eccc") //比第二种多一个输入框,第一个参数是标题,第二个参数是默认值
可以进行重写alert()方法
③使用window.open(url,name,features,replace)打开一个新窗口。 但是很明显在浏览器中禁止弹出新的窗口。
第四个参数表示新创建的窗口是在浏览器的历史记录中新创建一个条目,还是替换当前条目、
在打开的新创库中有有一个opener属性,保存着打开它的原始窗口对象。
④window中定义了focus()获取焦点,blur()放弃焦点。
⑤使用定时器
设置延迟多长时间执行函数
var timeOut=setTimeout(function(){
....
},1000)
clearTimeout(timeOut)
指定周期调用函数
setInterval()
clearInterval()
7.使用location对象,包括
href:声明了当前显示文档的完成的url
protocol
host
hostname
port
pathname
search:表示url的查询部分,也就是问号及以后的部分。例如:?id=23&name=eee
hash:
location还定义了两个方法:reload() replace()方法
reload():可以重新装在当前文档
replace():装在新的url后无需创建新的历史记录
window.location和document.location不同,前者是location对象,后者仅仅为一个只读字符串。