javascript 学习笔记 (二)

  1. 如果一行只有一个语句,建议在末尾使用分号 ;
  2. <script> ...</script> 告诉浏览器后面代码是javascript
  3. 函数: function name () { …} 和bash shell的函数定义完全一样的.
  4. 使用外部脚本 <script src="xxx.js"></script>
  5. window.onload = writeMessage 意思是将函数直接赋值给事件处理程序,以便在事件发生时运行它.
  6. 脚本中添加注释是一个良好习惯.
    单行注释 : //
function writeMessage() {
    // Here's where the actual work gets done

    document.getElementById("helloMessage").innerHTML = "Hello, world!";
}

多行注释 : /* …..*/

/*
    This is an example of a long JavaScript comment. Note the characters at the beginning and ending of the comment.

    This script adds the words "Hello, world!" into the body area of the HTML page.
*/

window.onload = writeMessage;   // Do this when page finishes loading

function writeMessage() {
    // Here's where the actual work gets done

    document.getElementById("helloMessage").innerHTML = "Hello, world!";
}
  • 发出警告 (” danger”)
  • 指出页面需要javascript <noscript>
    <h2>This page requires JavaScript.</h2>
    </noscript>

    条件选择:

    if (conditon)
    {A}
    esle
    {B}

    等同于: m=(conditon)?A:B;
  • 提示用户 promt(para1,para2)
    实例:

    // yes/no 是默认参数
    var ans=prompt("Are you sure you want to do that?","yes/no");
    if (ans) {
    alert("You said " + ans);
    }
    else {
    alert("You refused to answer");
    }
  • 对用户重定向
window.onload = initAll;   // 完成页面加载后执行initALL函数

function initAll() {
    document.getElementById("redirect").onclick = initRedirect;
}

/*
window.location(将浏览器中显示的页面设置为一个新页面)
*/
function initRedirect() {
    window.location = "jswelcome.html";   
    return false;   // 停止对用户单击的处理,不会加载href指向的页面
}
  • 改进链接
window.onload = initAll;

function initAll() {
    document.getElementById("redirect").onclick = initRedirect;
}
/* 脚本中的this可根据使用这个关键字的上下文将值传递给函数
比如:这里这个this 代表id=redirect 元素的内容*/
function initRedirect() {
    alert("We are not responsible for the content of pages outside our site");
    window.location = this;
    return false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值