java和html的结合方式
(1)使用script标签
<script type="text/javascript">javascript语句</script>
示例
html>
<head>
<title>
hello world
</title>
</head>
<body>
<script type="text/javascript">
alert("hello javascript");
</script>
</body>
</html>
显示效果为网页弹出了一个对话框。
(2)引入一个外部js文件
示例
js文件
alert("hello javascript");
html文件
<html>
<head>
<title>
hello world
</title>
</head>
<body>
<script type="text/javascript" src="1.js">
</script>
</body>
</html>
显示效果和方法一是一样的。
使用方法2的时候,如果继续在script标签页中输入javascript代码,是不会被执行的。
js的基本数据类型
1.String:字符串
var n=”hello world”;
2.number:数字类型
var n=1;
3.boolean:布尔类型
var n=true;
4.null
var data=new Data();
获取对象的引用,null表示对象引用为空
5.undifined
var n;
定义一个变量,没有赋值
查看当前变量数据类型的方法
typeof();
使用方法:typeof(变量名);返回数据类型
javascript中的语句
if,switch,while,for。和java中的使用方法是完全一致的
javascript的运算符
1.js中不区分整数和小数,所以12/100=0.12,而不是0。
2.字符串的加减,假设“456”+1,结果是4561;而“456”-1,结果为455。
3.“==”做的是值的比较,“===”做的是值和类型的比较。
4.向页面直接输出
document.write(“hello javascript”);//输出字符串
document.write(“
”);//输出html
javascript的数组
1.var m=[1,”22”,true];
2.var x=new Array(5);
x[0]=1; x[1]=”22”; x[2]=true;…..
3.var x=new Array(3,4,5);
相当于x[0]=3; x[1]=4; x[2]=5;
数组长度:数组名.length;
javascript的方法
1.
function 方法名(参数列表)
{
方法体;
返回值可有可无;
}
2.匿名函数
var b=function (参数列表)
{
方法体;
返回值可有可无;
}
通过b来进行调用
javascript的全局变量和局部变量
全局变量:在script标签中定义的变量,能够在同一页面中的js部分被调用
局部变量:在方法中定义的变量,只能在方法内部使用
javascript放置的位置
建议放置在body标签后面(因为html的解析是从上往下的,所以为了防止js想要获取的数据在html中都还没有解析到,最好放后面一点)
js的全局函数
1.eval();
如果形参是js代码块的字符串形式,则可以直接执行js代码
如 eval(“alert(“123”);”);
还有一些不常用的,可以直接查询全局函数。
js的函数重载
事实上,js里不存在函数重载,但是可以使用arguments变量来模拟函数重载
如
<script>
function add()
{
//传进function的形参都会被保存进arguments数组中(实际就是java中的args[])就可以使用arguments[n]来调用形参,arguments.length来获取形参个数
arguments[0].....arguments[n]
}
add(1,2,3);
add(1,2,3,4);
</script>
js的点击事件
示例
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
hello world
</title>
</head>
<body>
<input type="button" onclick="href1()" value="点击事件"/>
<script type="text/javascript">
function href1()
{
alert("hello");
}
document.write(navigator.appName);
</script>
</body>
</html>
点击页面上的 点击事件 按钮,就会执行js中的href1()函数,效果为弹出一个对话框,信息为hello
示例(点击事件跳转网页)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
hello world
</title>
</head>
<body>
<input type="button" value="点击事件" onclick="href1();"/>
</body>
<script type="text/javascript">
function href1()
{
alert("准备跳转");
self.location.href="2.html";
}
document.write(navigator.appName);
</script>
</html>
bom对象 ,windows对象,定时器
open()函数,打开一个新窗口,可以设置大小
var win=window.opener 返回创建当前页面的页面的对象
(可以用来进行页面间传值 win.decument.getElementById(“想要传递到的对象名”).value=”想要传的值”)
window.close();关闭当前页面
var d1=setInterval(“alert(‘hello’);”,3000);
每隔3秒,弹出一个信息框
var d2=setTimeout(“alert(‘hello’);”,3000);
在3秒后,弹出一个对话框(只有一次)
ClearInterval(d1);
ClearTimeout(d2);
清除相应的定时器
dom对象
文档对象模型
document对象
1.write();在网页上输出
2.getElementById();根据id获取元素(标签)
示例
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
hello world
</title>
</head>
<body>
<input type="text" value="hello world" id="hello">
</body>
<script type="text/javascript">
var x=document.getElementById("hello");
alert(x.value);
</script>
</html>
3.getElementsByName();根据Name获取元素(标签),返回的是一个集合
示例
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
hello world
</title>
</head>
<body>
<input type="text" value="hello world" name="a">
<input type="text" value="hello java" name="a">
<input type="text" value="hello html" name="a">
<input type="text" value="hello jsp" name="a">
</body>
<script type="text/javascript">
var x=document.getElementsByName("a");
alert(x.length);
</script>
</html>
4.getElementsByTagName();根据标签获取元素(标签),返回的是一个集合

546

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



