javascript-window对象的使用-04

本文深入讲解了JavaScript中Window对象的基本概念及其常用方法,包括警告框、输入数据、执行确认、弹出窗口、跳转操作等,并通过实例展示了如何在网页中运用这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在整个JavaScript之中Window是最基础的一个对象,也是使用最多的,下面来分别看window操作


范例:警告框——window.alert
<script type="text/javascript">
window.alert("hello");
</script>

范例:输入数据——window.prompt
<html>
<head>
<title>JavaScript应用</title>
<script type="text/javascript">
    var str=window.prompt("请输入数据","就在这里输入");
    document.write(str);
</script>
</head>
<body>
</body>
</html>

范例:执行确认——window.confirm
<html>
<head>
<title>JavaScript应用</title>
<script type="text/javascript">
function deleteInfo(){
    //此操作返回布尔值,如果确定返回时true,否则返回false
    return window.confirm("确定要删除吗");
    }
</script>
</head>
<body>
    关于XX的处罚决定!<a href="welcome.htm" onclick="return deleteInfo()">删除</a>
</body>
</html>

包含在实际之中还有一种确认提交表单。例如:用户填写完表单之后,会出现一个确认框,询问用户是否真的
要进行提交。

一种:
<html>
<head>
<title>JavaScript应用</title>
<script type="text/javascript">
function validate(){
    //此操作返回布尔值,如果确定返回时true,否则返回false
    return window.confirm("确定要提交吗");
    }
</script>
</head>
<body>
    <form action="welcome.htm" method="post" id="myform"
    onsubmit="return validate()">
    用户名:<input type="text" value="zhangsan"><br>
    <input type="submit" value="提交">
    </form>
</body>
</html>

两种:
<html>
<head>
<title>JavaScript应用</title>
<script type="text/javascript">
function validate(){
    //此操作返回布尔值,如果确定返回时true,否则返回false
    if(window.confirm("确定要提交吗")){
    document.getElementById("myform").submit();
    }
    }
</script>
</head>
<body>
    <form action="welcome.htm" method="post" id="myform">
    用户名:<input type="text" value="zhangsan"><br>
    <input type="button" value="提交" onclick="validate()">
    </form>
</body>
</html>

范例:弹出窗口——window.open()
<script type="text/javascript">
    window.open("welcome.htm")
</script>
但是这个时候发现,弹出来的框带的东西有点多。那么可以设置一些属性进行控制。

<script type="text/javascript">
    window.open("welcome.htm","广告","width=500,height=400,scrollbars=no,
    resizable=no");
</script>


范例:跳转操作——window.location
<script type="text/javascript">
    window.location="welcome.htm";
</script>


那么除了以上的功能之外,还会存在一个父子窗口问题。例如:A页面,打开了B页面,那么B页面就是A页面的
子窗口,同时B页面如果要想操作A页面中的对象则必须在前面加上“opener()”

范例:定义一个页面打开另外一个页面
<html>
<head>
<title>JavaScript应用</title>
<script type="text/javascript">
    function setValue(){
        window.open("city.htm","width=500,height=400");
    }
</script>
</head>
<body>
    <input type="text" id="city" name="city"disabled >
    <input type="button" value="选择城市"
    onclick="setValue()">
</body>
</html>

范例:定义一个页面打开另外一个页面
<html>
<head>
<title>JavaScript应用</title>
<script type="text/javascript">
    function setValue(){
        window.open("city.htm","width=500,height=400");
    }
</script>
</head>
<body>
    <input type="text" id="city" name="city"disabled >
    <input type="button" value="选择城市"
    onclick="setValue()">
</body>
</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值