html当中如何引用js文件
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
马克-to-win:如果需要javascript工程师和html美工各干各的工作,需要分开写文件。
例 1.2
<html>
<head>
<script src="Hello.js"></script>
<title></title>
</head>
<body>
</body>
</html>
Hello.js(如果你用notepad建立一个txt之后你再改为js,一定在存时,要存成utf-8或unicode格式):
var a ;
/*before you set value, a' type can not be defined.*/
document.writeln(typeof(a) + "<br>他们");
a = true;
document.writeln(typeof(a) + "<br>");
/*下面的console.log只有安装了firebug的firebox不报错*/
console.log("This is the 1 message 马克-to-win write in %s", a);
document.writeln(a);
2.简单语法:
例 2.1
<html>
<head>
</head>
<body>
<script type="text/javascript">
var i = 0;
do
{
i += 1;
window.document.write("i=" + i + "<br>");
}
while (i < 3)
</script>我们
</body>
</html>
输出结果:
i=1
i=2
i=3
我们
例 2.2
<!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"/>
<script type="text/javascript">
var i = 5;
while (i > 0) {
window.document.write("i is" + i + "<br>");
i--;
}
</script>
</head>
<body>
</body>
</html>
输出结果:
i is5
i is4
i is3
i is2
i is1
例 2.3(自动转化,比如3<"4")
<html>
<head>
<script type="text/javascript">
result = 0;
function qiuhe()
{
var num = window.document.getElementById("num").value;
/*the following typeof(num) is a string. can automatically convert string to number.*/
alert("typeof(num) is "+typeof(num));
for (var i = 1; i <= num; i++)
{
result = result+i;
}
window.document.getElementById("result").value = result;
}
</script>
</head>
<BODY>
1加到
<input type="text" id="num" size="5">和是 <br>
<input type="text" id="result">
<input type="submit" onclick="qiuhe();" value="cal">
</BODY>
</html>
例 2.4
马克-to-win:javascript中,当作为字符串时,单引号和双引号是通用的,都行。如下面的例子:如二者同时用,才需要配对。比如: <BODY onload='a("a","b","c")'>
<html>
<head>
<script type="text/javascript">
var sex = 'female';
if (sex == "female")
this.document.writeln("小姐");
else
this.document.writeln('先生');
</script>
</head>
<body>
</body>
</html>
输出结果:
小姐
例 2.5
<!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></title>
<script type="text/javascript">
document.write("hello World<br>");
with (document) {
write("hello world with with1<br>");
write("hello world with with.2");
}
</script>
</head>
<body>
</body>
</html>
输出结果:
hello World
hello world with with1
hello world with with.2
例 2.6
<!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"/>
<SCRIPT LANGUAGE="JavaScript">
<!--
var a = 12,b = 34,c = 56;
c = ++a + b;
//a值再加1为13,再加b34,c值为47
document.writeln("c=" + c);
document.writeln("b=" + b);
document.writeln("a=" + a);
//-->
</SCRIPT>
<title></title>
</head>
<body>
</body>
</html>
输出结果:
c=47 b=34 a=13
例 2.7(Document.write语法)
<!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></title>
</head>
<body>
this content is before document.write <br>
<script type="text/javascript">
document.write("<hr size=2 color='orange' width=50% align=left>");
for (var i = 5; i > 0; i--)
{
if (i == 3) continue;
document.write("i=" + i + "<br>");
}
</script>
<br>this content is after document.write <br>
</body>
</html>
输出结果:
this content is before document.write
i=5
i=4
i=2
i=1
this content is after document.write
例 2.8(prompt)
<!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"/>
<Script>
/*JavaScript syntax: - myResult = myWindow.prompt(aString, aDefaultValue)
- myResult = prompt(aString, aDefaultValue)
Argument list: aDefaultValue An initial content for the text box
aString Some text to explain what to enter
*/
var yourgender = prompt("你的性別是:\n男生请按11,女生请按22", "22");
switch (yourgender) {
case "11" : document.writeln("你好!"); break;
case "22" : document.writeln("你好!woman"); break;
default : document.writeln("重按!");
}
</script>
</head>
<body>
</body>
</html>
结果;
你好!woman