您的位置:首页 > Web前端 > JavaScript

05-javascript基础回顾

2015-10-07 19:04 435 查看




<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="form1" action="test.html" method="post">
<input type="text" name="username" value="zhang">
<input type="button" name="ok" value="保存1">
</form>

<form name="form2" action="test1.html" method="post">
<input type="text" name="username" value="zhang2">
<input type="button" name="ok" value="保存2">
</form>
</body>
<script type="text/javascript">
//使用俩种方式输出表单的action值(使用表单的名称,使用表单数组forms)
//方法一
var formElement=document.form1;
alert(formElement.action);
alert(formElement.method);
//方法二
alert(document.forms[0].action);
alert(document.forms[0].method);
</script>
</html>

javascript中定义函数的三种方式
第一种方式---正常方式

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="form1" action="" method="post">
患者姓名:<input type="text" name="username" value="zhang"/>
<input type="button" name="ok" value="打印患者信息" onclick="printPerson();"/>
<input type="button" name="ok" value="查询患者信息" onclick="selectPerson();"/>
</form>
</body>
<script type="text/javascript">
//第一种定义函数的方法,正常方法
//通过JavaScript函数的方式访问printPerson.html和selectPerson.html
function printPerson(){
var formElement=document.forms[0];
formElement.action="printPerson.html";
formElement.method="post";
formElement.submit();
}
function selectPerson(){
var formElement=document.forms[0];
formElement.action="selectPerson.html";
formElement.method="get";
formElement.submit();
}
</script>
</html>

第二种方式---构造函数的方式(不常用)
//使用构造函数方法定义javascript函数
var add=new Function('a','b','return a+b;');
alert(add(3,4));

第三种方式---函数直接量定义函数(常用)
//匿名函数
var add=function(a,b){
return a+b;
}
alert(add(3,4));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: