Maison > interface Web > js tutoriel > le corps du texte

javascript基础第一章 JavaScript与用户端_基础知识

WBOY
Libérer: 2016-05-16 18:22:34
original
956 Les gens l'ont consulté

一 页面输出
1.头部文件

复制代码 代码如下:





2.页面内
复制代码 代码如下:


<script> <BR>document.write("脚本之家"); <BR></script>


3.外部文件

4.利用页面ID的innerHtml
复制代码 代码如下:

<script> <BR>window.onload = writeMessage; // 页面加载时调用writeMessage函数 <BR>writeMessage() { <BR>document.getElementById("helloMessage").innerHTML = "脚本之家"; <BR>//找到dom ID(helloMessage),修改其html内容 <BR>} <BR></script>

5.警告
alert("广州百汇物流有限公司");
6.询问
复制代码 代码如下:

if (confirm("是否访问我们的首页"))
{
alert("是的,前往");
}
else {
alert("退出");
}

7.输入
复制代码 代码如下:

var ans = prompt("输入你的留言","您好,");
if (ans) {
alert("你说:" + ans);
}
else {
alert("退出,没有留言");
}

8.页面跳转
复制代码 代码如下:

<script> <BR>window.onload = initAll; <BR>function initAll() { <BR>document.getElementById("redirect").onclick = initRedirect; <BR>} <BR>function initRedirect() <BR>{ <BR>window.location = "index.html"; <BR>return false; <BR>} <BR></script>
脚本之家

9.判断分支
复制代码 代码如下:

<script> <BR>window.onload = initAll; <BR>function initAll() { <BR>document.getElementById("Lincoln").onclick = saySomething; <BR>document.getElementById("Kennedy").onclick = saySomething; <BR>document.getElementById("Nixon").onclick = saySomething; <BR>} <BR>function saySomething() { <BR>switch(this.id) { <BR>case "Lincoln": <BR>alert("Four score and seven years ago..."); <BR>break; <BR>case "Kennedy": <BR>alert("Ask not what your country can do for you..."); <BR>break; <BR>case "Nixon": <BR>alert("I am not a crook!"); <BR>break; <BR>default: <BR>} <BR>} <BR></script>


type="button" id="Kennedy" value="Kennedy" />
value="Nixon" />


10.异常捕获
复制代码 代码如下:

window.onload = initAll;
function initAll() {
var ans = prompt("输入参数:","");
try {
if (!ans || isNaN(ans) || ansthrow new Error("输入为非数");
}
alert("根号" + ans + " 是 " + Math.sqrt(ans));
}
catch (errMsg) {
alert(errMsg.message);
}
}
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!