window对象的常用方法
- window对象的常用方法不用通过window调用,可以直接点出来
- prompt():输入框,用户可以根据提示输入内容
- alert():消息框,给出用户提示,可以用来测试数据
- confirm():消息框,有确认和取消两个按钮,返回值为trun或false
- close():关闭浏览器窗口
- open():使用新的浏览器窗口打开指定的URL,可以指定窗口名称和特征
<script>
function a(){
//弹出确认窗口
var sf=confirm("是否关闭");
if(sf){
open("about:blank","_self").close();
}else{
alert("用户取消了操作")
}
- setTimeout():在指定毫秒后调用某个函数
- setInterval():在指定以毫秒为周期调用函数
setInterval("sj()",1000);
window对象的常用属性
- history:用户访问过的(以前的)URL信息
- location:当前的URL信息
history对象
- history对象的常用方法:
- back():返回history对象列表的上一个URL
<a href="javascript:history.back()">返回上一页</a>
- forward():加载history对象列表的下一个URL
- go():加载列表中某个具体的URL (-1时相当于back();1时相当于forward());
location对象
-location的常用属性
- host:设置或返回主机名和当前URL的端口号
- hostname:设置或返回当前URL的主机名
- href:设置或返回完整的URL
- 常用方法
- reload():重写加载当前文档
- replace():用新的文档替换当前文档(在当前窗口加载新文档)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
document.getElementById("ad").innerHTML="";
alert(location.host);
alert(location.hostname);
alert(location.href);
//设置属性
//location.host=60000;
//返回
var duan = location.host;
</script>
</head>
<body>
<p onclick="javascript:location.reload()">刷新网页</p>
<p onclick="javascript:location.replace('http://www.baidu.com')">打开百度</p>
</body>
</html>
Document对象
- 常用方法:
- getElementByid():返回指定id的对象的引用(通过id找到这个标签)
- getElementsByName():返回指定name的对象的集合(通过name找到标签,返回一个集合)
- getElementsByTagName():返回 指定标签名的对象集合(通过标签名找到此元素的集合)
- write():向网页中写入文本或者代码
<script>
function change(){
//获取标签对象
document.getElementById("a1").innerHTML="<h1>这是div中显示的文字</h1>";
document.getElementById("a1").style.backgroundColor="red";
document.getElementById("a1").style.height="60px";
document.getElementsByTagName("p")[1].innerHTML="比较方便";
//获取输入框中的数据
var a= document.getElementsByName("uname")[0].value;
alert(a);
document.gete
}
</script>