第二章.条件语句与交互事件(链接事件、图片交互)
逻辑与 &&
逻辑或 ||
比较运算符 == != > < <=
赋值运算符 =
1.条件语句 [if-then-else]
var monkey_love = prompt("Do you love the monkey?","Type yes or no");
if (monkey_love == "yes")
{
alert("Welcome! I'm so glad you came! Please, read on!");
}
2. switch函数
switch (expression)
{
case label1: 语句串1;
case label2: 语句串2;
case label3: 语句串3;
...
default: 语句串3;
}
3.for 语句
for (初始化部分;条件部分;更新部分)
{
执行部分...
}
4.for in 语句
for (变量 in 对象或数组)
{
语句...
}
5.while语句
while (条件)
{
执行语句...
}
6.with
with (对象名称)
{
执行语句
}
7.事件
onClick 点击
onMouseOver 移上
onMouseOut 移出
onLoad 加载
onUnload 卸载
onBlur 失去焦点 //文本域
onFocus 获得焦点//文本域
onChange 变值失焦//文本域
8.图片交互
<img src="button_r.gif" name="the_image">
/* 这就象一个标准的 <img src= > 标签,只是它被
给了一个名字: the_image. 名字是任意取的:
my_image, a_box,.. ..但不许有任何空格
在里面
*/
<a href="#" onMouseOver="document.the_image.src='button_d.gif';">change</a>
/* 该句是说:“找到叫'the_image'的图片并将其src 变为
button_d.gif." 注意整个语句使用双引号,而
'button_d.gif' 使用单引号。尽管二者可互换,但注意
如果一组引号存在于另一组引号之中,别搞混了。你
可写成" 'something' " 或 ' "something" ' ,但不可
写成:" 'something" ' or " "something" ".
*/