javascript介绍及其简单例子(5)

设置网页的属性

document可以进行一些简单网页的设置,如网页标题.颜色等,并且可以得到网页的某些属性,比如当前地址。比较常用的有:通过“document.title”等来访问标题,通过document.lcation来获取网页的当前地址等!下面举一个例子

<html>
<body>
<script type="text/javascript">
function fun(){
document.title="新的标题";//设置网页标题
window.alert(document.location);//得到当前网页地址
}
</script>
<input type="button"οnclick="fun()"value="按钮">
</body>
</html>



虾米那写一下有关访问文档元素,特别是表单元素

document可以访问文档中的元素(如图片。表单。表单中的控件),前提是元素的name属性确定。访问的方法为;

"document.元素名.子元素名..."比如form1的表单中有一个文本框account,其中的 内容可以如下代码获得:

var account=document.form1.account.value;

下面就介绍一下例子:

<html>
<body>
<script type="text/javascript">
function add(){
n1=Number(document.form1.txt1.value);
n2=Number(document.form1.txt2.value);
document.form1.txt3.value=n1+n2;
}
</script>
<form type="form1">
<input name="txt1" type="text"><BR>
<input name="txt2" type="text"><BR>
<input type="button" οnclick="add()" value="求和"><BR>
<input name="txt3" type="text"><BR>
</form>
</body>
</html>



由于document可以得到网页中的元素的值,所以在客户端的验证中用的非常广,比如在登陆或者注册中,可以使用JavaScript得到表单中的值,然后通过判断,做出相应的反应。下面就介绍一个例子:

<html>
<body>
<script type="text/javascript">
function validate(){

//得到文本内容
account=document.loginForm.account.value;
password=document.loginForm.password.value;
if(account==""){
alert(" 账号不能为空");
document.loginForm.account.focus();//聚焦
return;
}
else if(password==""){
alert(""密码不能为空);
document.loginForm.password.focus();
return;
}
document.loginForm.submit();
}
</script>
欢迎您登陆:
<form name="loginForm">
输入账号:<input name="account" type="text"><BR>
输入密码:<input name="password" type="text"><BR>
<input type="button"οnclick="validate()" value=" 登陆">
</form>
</body>
</html>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值