用function定义函数:
function test(a) {
document.write(a);
}
。。。。
test(”函数调用“);
onfocus获取焦点事件:
在本例中,当输入框获得焦点时,其背景颜色将改变:
<html>
<head>
<script type="text/javascript">
function setStyle(x)
{
document.getElementById(x).style.background="yellow"
}
</script>
</head>
<body>
First name: <input type="text"
onfocus="setStyle(this.id)" id="fname" />
<br />
Last name: <input type="text"
onfocus="setStyle(this.id)" id="lname" />
</body>
</html>
输出:
First name:
Last name:
<input type="text" name="username" value="test" onFocus="alert(document.jb.username.value);">
onLoad一般写下body里。
onsubmit为按钮提交之前触发,却写到from里面。οnsubmit=“return false”可进行表单验证。
本文介绍了JavaScript中函数定义及调用的方法,并通过实例展示了如何使用onfocus事件来改变HTML输入框的背景颜色,以及如何利用onLoad和onsubmit事件进行页面加载和表单验证。

被折叠的 条评论
为什么被折叠?



