javascript基础第一章 JavaScript与用户端
更新时间:2010年07月22日 13:27:32 作者:
javascript基础第一章 JavaScript与用户端
一 页面输出
1.头部文件
<head>
<script language="javascript">
document.write("脚本之家www.jb51.net");
</script>
</head>
2.页面内
<body>
<script>
document.write("脚本之家");
</script>
</body>
3.外部文件
<script src="display.js"></script>
4.利用页面ID的innerHtml
<script>
window.onload = writeMessage; // 页面加载时调用writeMessage函数
writeMessage() {
document.getElementById("helloMessage").innerHTML = "脚本之家";
//找到dom ID(helloMessage),修改其html内容
}
</script>
5.警告
alert("广州百汇物流有限公司");
6.询问
if (confirm("是否访问我们的首页"))
{
alert("是的,前往");
}
else {
alert("退出");
}
7.输入
var ans = prompt("输入你的留言","您好,");
if (ans) {
alert("你说:" + ans);
}
else {
alert("退出,没有留言");
}
8.页面跳转
<script>
window.onload = initAll;
function initAll() {
document.getElementById("redirect").onclick = initRedirect;
}
function initRedirect()
{
window.location = "index.html";
return false;
}
</script>
<a href="https://www.jb51.net" id="redirect">脚本之家</a>
9.判断分支
<script>
window.onload = initAll;
function initAll() {
document.getElementById("Lincoln").onclick = saySomething;
document.getElementById("Kennedy").onclick = saySomething;
document.getElementById("Nixon").onclick = saySomething;
}
function saySomething() {
switch(this.id) {
case "Lincoln":
alert("Four score and seven years ago...");
break;
case "Kennedy":
alert("Ask not what your country can do for you...");
break;
case "Nixon":
alert("I am not a crook!");
break;
default:
}
}
</script>
<form action="#">
<input type="button" id="Lincoln" value="Lincoln" />
<input
type="button" id="Kennedy" value="Kennedy" />
<input type="button" id="Nixon"
value="Nixon" />
</form>
10.异常捕获
window.onload = initAll;
function initAll() {
var ans = prompt("输入参数:","");
try {
if (!ans || isNaN(ans) || ans<0) {
throw new Error("输入为非数");
}
alert("根号" + ans + " 是 " + Math.sqrt(ans));
}
catch (errMsg) {
alert(errMsg.message);
}
}
1.头部文件
复制代码 代码如下:
<head>
<script language="javascript">
document.write("脚本之家www.jb51.net");
</script>
</head>
2.页面内
复制代码 代码如下:
<body>
<script>
document.write("脚本之家");
</script>
</body>
3.外部文件
<script src="display.js"></script>
4.利用页面ID的innerHtml
复制代码 代码如下:
<script>
window.onload = writeMessage; // 页面加载时调用writeMessage函数
writeMessage() {
document.getElementById("helloMessage").innerHTML = "脚本之家";
//找到dom ID(helloMessage),修改其html内容
}
</script>
5.警告
alert("广州百汇物流有限公司");
6.询问
复制代码 代码如下:
if (confirm("是否访问我们的首页"))
{
alert("是的,前往");
}
else {
alert("退出");
}
7.输入
复制代码 代码如下:
var ans = prompt("输入你的留言","您好,");
if (ans) {
alert("你说:" + ans);
}
else {
alert("退出,没有留言");
}
8.页面跳转
复制代码 代码如下:
<script>
window.onload = initAll;
function initAll() {
document.getElementById("redirect").onclick = initRedirect;
}
function initRedirect()
{
window.location = "index.html";
return false;
}
</script>
<a href="https://www.jb51.net" id="redirect">脚本之家</a>
9.判断分支
复制代码 代码如下:
<script>
window.onload = initAll;
function initAll() {
document.getElementById("Lincoln").onclick = saySomething;
document.getElementById("Kennedy").onclick = saySomething;
document.getElementById("Nixon").onclick = saySomething;
}
function saySomething() {
switch(this.id) {
case "Lincoln":
alert("Four score and seven years ago...");
break;
case "Kennedy":
alert("Ask not what your country can do for you...");
break;
case "Nixon":
alert("I am not a crook!");
break;
default:
}
}
</script>
<form action="#">
<input type="button" id="Lincoln" value="Lincoln" />
<input
type="button" id="Kennedy" value="Kennedy" />
<input type="button" id="Nixon"
value="Nixon" />
</form>
10.异常捕获
复制代码 代码如下:
window.onload = initAll;
function initAll() {
var ans = prompt("输入参数:","");
try {
if (!ans || isNaN(ans) || ans<0) {
throw new Error("输入为非数");
}
alert("根号" + ans + " 是 " + Math.sqrt(ans));
}
catch (errMsg) {
alert(errMsg.message);
}
}
相关文章
关于javascript中的typeof和instanceof介绍
typeof用来检测给定变量的数据类型 instanceof用来检测对象的类型2012-12-12
Javascript实例教程(19) 使用HoTMetal(7)
Javascript实例教程(19) 使用HoTMetal(7)...2006-12-12
javascript setTimeout和setInterval 的区别
window对象有两个主要的定时方法,分别是setTimeout 和 setInteval 他们的语法基本上相同,但是完成的功能取有区别。2009-12-12
JavaScript中Date.toSource()方法的使用教程
这篇文章主要介绍了JavaScript中Date.toSource()方法的使用教程,用来返回日期为字符串,是JS入门学习中的基础知识,需要的朋友可以参考下2015-06-06


最新评论