码迷,mamicode.com
首页 > 编程语言 > 详细

学习笔记.JavaScript开始(二)

时间:2015-03-18 18:31:51      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:基础   javascript   

1.<script>标签的属性,方法及事件处理程序;

属性:

defer(defer):规定是否对脚本执行延迟,直到页面加载为止;

src(url):规定外部脚本文件的url;

charset(charset):规定外部脚本文件使用的字符编码;

async:规定异步执行脚本;

event:设定或获取脚本编写用于的事件;

htmlFor:设定或获取绑定到事件脚本的对象;

type、language属性已在标准中取消。

方法:无

事件:onerror、onload。

2.带圆括号的函数名意味着正在调用这个函数;如果没有圆括号,就是将它赋值给事件处理程序,以便此事件发生时运行它。

3.大段的注释采用/* XXXXX */方式,行内注释采用//XXXX方式。

4.<noscript>标签在不支持JavaScript的浏览器上,会显示一条消息,它执行标签内的内容。

<noscript>
    <h2>This page requires JavaScript.</h2>
</noscript>

5.确认用户的选择。  

if(confirm("Are you want to do that?")){
    alert("You said yes");
}
else{
    alert("You said no");
}

confirm()方法用于显示一个带有指定消息和 OK 及取消按钮的对话框。
confirm(message),确定返回true,取消返回false。

(confirm("Are you want to du that?"))?alert("You said yes"):alert("You said no");

6.提示用户。

var ans = prompt("Are you sure you want to du that?","Default answer...");
if(ans){
    alert("You said " + ans);
}
else{
    alert("You refused to answer");
}

传递给prompt()方法的是由逗号分隔的两段信息:向用户询问的问题和默认的回答。方法返回用户的响应或null。

7.用链接对用户进行重定向。

window.onload = initAll;
function initAll(){
    document.getElementById("redirect").onclick = initRedirect;
}
function initRedirect(){
    window.location = "http://www.baidu.com";
    return false;
}

window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。

window.location = "http://www.baidu.com"  相当于

window.location.href = "http://www.baidu.com"

8.使用JavaScript改进链接。

window.onload = initAll;
function initAll(){
    document.getElementById("redirect").onclick = initRedirect;
}
function initRedirect(){
    //alert("We are not responsible for the content of pages outside our site.");
    if(confirm("Go on!")){
        window.location.href = this;
        return false;
    }
    else{
        window.location.href = window.location;
    }
}

9.使用多级条件。

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

10.处理错误。

<!DOCTYPE html>
<html>
<head>
    <title>Throw wrong</title>
</head>
<body>
    <h2>Compute square</h2>
    <script>
        window.|| isNaN(ans) || ans<0){
                    throw new Error("Not a valid number.");
                }
                alert("The square root of " + ans + " is " + Math.sqrt(ans));
            }
            catch(errMsg){
                alert(errMsg.message);
            }
        }
    </script>
</body>
</html>

try:错误测试代码块;

throw:自定义错误提示;

catch(errMsg):发生错误时执行的代码。

本文出自 “技术无巅峰” 博客,请务必保留此出处http://shelia.blog.51cto.com/2930476/1621787

学习笔记.JavaScript开始(二)

标签:基础   javascript   

原文地址:http://shelia.blog.51cto.com/2930476/1621787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!