首页 > 代码库 > JavaScript 基础知识

JavaScript 基础知识

浏览器的对象树:

 

1:window对象常用方法:

*alert(‘信息’) :      消息框
*prompt(‘提示信息’,默认值): 标准输入框
*confirm( )  :      确认框
*open( )      :      打开一个新窗口
*close( )      :     关闭窗口
 

2:Form表单对象

访问表单的方式:
* document.forms[n]
* document.表单名字  (这里是name属性)
表单对象常用的属性
action <from action=”xxx”> 表单提交的目的地址
method <form method=”xxx”> 表单提交方式
name <form name=”xxx”> 表单名称

不同的按钮跳转不同的action  JS动态的改变action 的地址

<!DOCTYPE html><html>  <head>    <title>test.html</title>	    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="this is my page">    <meta http-equiv="content-type" content="text/html; charset=UTF-8">        <!--<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">-->	<script>		function show(target) {			var formObj = document.forms[0];			formObj.action = target;			doSubmit(formObj);		}		function doSubmit(form) {			form.method = "post";			form.submit();		}			</script>  </head>      <body>     <form action="" method="">     	<input type="button" value="http://www.mamicode.com/展示1" onclick="show(‘1.jsp‘)">		<input type="button" value=http://www.mamicode.com/展示2 onclick="show(‘2.jsp‘)">     </form>  </body></html>

  

 

3: javaScript定义函数的三种方式

(1)

正常方法
function print(msg){
  document.write(msg);
}

对函数进行调用的几种方式:
函数名(传递给函数的参数1,传递给函数的参数2,….)
变量 = 函数名(传递给函数的参数1,传递给函数的参数2,….)
对于有返回值的函数调用,也可以在程序中直接使用返回的结果,例如:alert("sum=“ + square(2,3));
不指定任何函数值的函数,返回undefined。

 

(2)

构造函数方法 new Function();
//构造函数方式定义javascript函数 注意Function中的F大写
var add=new Function(‘a‘,‘b‘,‘return a+b;‘);

//调用上面定义的add函数
var sum=add(3,4);
alert(sum);
注:接受任意多个字符串参数,最后一个参数是函数体。
如果只传一个字符串,则其就是函数体。

 

(3)

函数直接量定义函数
//使用函数直接量的方式定义函数
var result=function(a,b){return a+b;}

//调用使用函数直接量定义的函数
var sum=result(7,8);
alert(sum);
注:函数直接量是一个表达式,它可以定义匿名函数

 
<script>		//1  普通方法		function add(a, b){			//alert("a = " + a + " : " + "b = " + b);			return a + b;		}		//alert(add(3,4));				//2 构造函数方法 		var add = new Function(‘a‘, ‘b‘, ‘c‘,‘return a + b + c‘);		//alert(add(3,4,5));				//3 函数直接量定义函数		var add = function(a, b) {			return a + b;		}		alert(add(3,4));			</script>