zoukankan
html css js c++ java
javascript权威指南例子(自己写的)
javascript权威指南例子(自己边看边写的)
前面几章的
下载
<!
DOCTYPE HTML PUBLIC
"
-//W3C//DTD HTML 4.0 Transitional//EN
"
>
<
HTML
>
<
HEAD
>
<
TITLE
>
New Document
</
TITLE
>
</
HEAD
>
<
BODY
>
<!--
sample
1
-->
<
script language
=
"
javascript
"
>
document.write(
"
<h1>hello this is Factorials</h1>
"
)
for
(i
=
0
;i
<
10
;i
++
)
{
document.write(i
+
"
*
"
+
i
+
"
=
"
+
i
*
i);
document.write(
"
<br />
"
)
}
</
script
>
<!--
sample
1
一般的事件
-->
<
input type
=
"
button
"
value
=
"
click here (eventhander)
"
onclick
=
"
alert('you clicked this button!');
"
></
input
>
<!--
sample
2
函数调用 无返回值
-->
<
br
/>
<
h3
>
sample
2
函数调用 无返回值
</
h3
><
br
/>
<
script language
=
"
javascript
"
>
function function1()
{
alert(
"
good
"
);alert(
"
yes
"
);
//
语句在不同的行中可以不写分号,建议只要是语句都要写分号。
alert(
"
1
"
)
alert(
"
2
"
)
/**/
/*
注释
*/
}
</
script
>
<
br
/>
<
input type
=
"
button
"
value
=
"
function1
"
onclick
=
"
function1();
"
></
input
>
<!--
sample
3
求平方根
-->
<
br
/>
<
h3
>
sample
3
求平方根
</
h3
><
br
/>
<
script language
=
"
javascript
"
>
function countsqrt()
{
result
=
Math.sqrt(document.getElementById(
"
numsqrt
"
).value);
//
Math.sqrt 计算平方根
document.getElementById(
"
numsqrt
"
).value
=
result;
//
把计算结果赋值给文本框
alert(result);
//
alert((255).toString(0x10))
}
</
script
>
<
input type
=
"
text
"
id
=
"
numsqrt
"
value
=
"
2
"
></
input
>
<
input type
=
"
button
"
id
=
"
countsqrt
"
value
=
"
计算平方根
"
onclick
=
"
countsqrt();
"
></
input
>
<!--
sample
4
特殊数值的常量
-->
<
script language
=
"
javascript
"
>
document.write(
"
<br /> <h3>sample 4 特殊数值的常量</h3><br />
"
)
document.write(
"
Number.MAX_VALUE:
"
+
Number.MAX_VALUE
+
"
<br />
"
);
document.write(
"
Number.MIN_VALUE:
"
+
Number.MIN_VALUE
+
"
<br />
"
);
document.write(
"
Number.MIN_VALUE:
"
+
Number.MIN_VALUE
+
"
<br />
"
);
document.write(
"
Number.NaN
"
+
Number.NaN
+
"
<br />
"
);
document.write(
"
Number.POSTIVE_INFINITY
"
+
Number.POSTIVE_INFINTITY
+
"
\n\r\f1111<br />
"
);
</
script
>
<!--
sample
5
字符串常用操作
-->
<
script language
=
"
javascript
"
>
document.write(
"
<br /> <h3>sample 5 字符串常用操作</h3><br />
"
)
msg
=
"
wuyisky
"
;
document.write(msg.length);
function StringOperate()
{
var index
=
document.getElementById(
"
Operate
"
).value;
var word
=
document.getElementById(
"
stringInput
"
).value;
switch
(index)
{
case
"
getLength
"
:
alert(
"
\
""
+word+
"
\
"
的长度是:
"
+
word.length);
break
;
case
"
getLastChar
"
:
alert(
"
\
""
+word+
"
\
"
的最后一个字符是:
"
+
word.charAt(word.length
-
1
));
//
charAt:返回指定索引位置处的字符。strObj.charAt(index); strObj 是string类型,index: from 0 to strObj.length-1
alert(
"
substring(2)结果是:
"
+
word.substring(
2
)
+
"
// 从参数的索引位置开始到结尾的子字符串
"
);
alert(
"
word.substring(2,4)结果是:
"
+
word.substring(
2
,
4
)
+
"
// 从参数1的索引位置开始到参数2的索引位置结束
"
);
alert(
"
word.substr(2,4)结果是:
"
+
word.substr(
2
,
4
)
+
"
// 从参数1的索引位置开始的参数2个字符
"
);
alert(
"
word.slice(2,4)结果是:
"
+
word.slice(
2
,
4
)
+
"
// 从参数1的索引位置开始的参数2个字符
"
);
break
;
default
:
alert(word);
}
}
</
script
>
<
input type
=
"
text
"
id
=
"
stringInput
"
value
=
"
被操作的字符串
"
></
input
>
<
select id
=
"
Operate
"
onchange
=
"
StringOperate();
"
>
<
option value
=
"
getLength
"
>
求长度
</
option
>
<
option value
=
"
getLastChar
"
>
取最后一个字符
</
option
>
<
option value
=
"
2
"
>
求长度
</
option
>
<
option value
=
"
3
"
>
求长度
</
option
>
</
select
>
<!--
sample
6
函数直接量
-->
<
script language
=
"
javascript
"
>
document.write(
"
<br /> <h3>sample 6 函数直接量</h3><br />
"
)
var f
=
function(x)
{
return
x}
;
var s
=
f(
"
函数直接量;
"
)
document.write(s);
</
script
>
<!--
sample
7
对象直接量
-->
<
script language
=
"
javascript
"
>
document.write(
"
<br /> <h3>sample 7 对象直接量</h3><br />
"
)
var point
=
new
Object();
//
create a Object
point.x
=
2
;
//
属性x
point.y
=
3
;
//
属性y
document.write(point.x.toString()
+
point.y.toString());
var point1
=
{x:
2
,y:
3
}
;
//
对象直接量
document.write(point1.x.toString()
+
point1.y.toString());
</
script
>
<!--
sample
8
数组的使用
-->
<
script language
=
"
javascript
"
>
document.write(
"
<br /> <h3>sample 8 数组的使用</h3><br />
"
)
var a
=
new
Array();
a[
0
]
=
1
;
a[
1
]
=
"
wuyi
"
;
a[
2
]
=
true
;
a[
3
]
=
{x:
1
,y:
"
a string
"
}
;
for
(i
=
0
;i
<=
3
;i
++
)
{
document.write(
"
<br />
"
+
a[i]);
}
document.write(a[
3
].x
+
a[
3
].y);
</
script
>
<!--
sample
9
数组的使用
-->
<
br
/>
<
h3
>
sample
9
函数中声明的所有变量在整个函数中都有定义
</
h3
><
br
/>
</
BODY
>
</
HTML
>
查看全文
相关阅读:
程序员面试金典-面试题 16.15. 珠玑妙算
程序员面试金典-面试题 16.14. 最佳直线
程序员面试金典-面试题 16.13. 平分正方形
程序员面试金典-面试题 16.11. 跳水板
程序员面试金典-面试题 16.10. 生存人数
程序员面试金典-面试题 16.08. 整数的英语表示
程序员面试金典-面试题 16.07. 最大数值
程序员面试金典-面试题 16.06. 最小差
python学习笔记-47 UDP编程
python学习笔记-46 TCP编程
原文地址:https://www.cnblogs.com/wuyisky/p/913395.html
最新文章
最长回文算法2
散列表保存多项式链表相乘的结果
最长回文(通过比较原字符串和其翻转字符串)
开放定址散列表
散列表(冲突用链表解决)
最优二叉树搜索
最长公共子序列
队列和栈
【interview】2020.07.20
【前端小技巧】
热门文章
【WebSocket】01
【interview】2020.07.18 try
【interview】2020.07.16 vue
【interview】2020.07.11 前端性能优化相关
【interview】2020.07.11 数据结构和算法
【mac】mac 安装 硬盘
【interview】2020.07.09
【interview】2020.07.08
程序员面试金典-面试题 16.17. 连续数列
程序员面试金典-面试题 16.16. 部分排序
Copyright © 2011-2022 走看看