什么是javascript:
JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果。
什么是jQuery:
jQuery是一个优秀的JavaScript库,提供许多封装好的功能。
javascript 在网页中存在的方式:
当前页面引用js
<script type="text/javascript"></script>
引用文件js
1
2
3
4
5
6
7
8
9
10
11
|
<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
charset
=
"UTF-8"
content
=
"text/html"
>
<
title
>js_st1</
title
>
<
script
src
=
"js/tuchao1.js"
></
script
>
</
head
>
<
body
>
</
body
>
</
html
>
|
注释语法:
单行注释 //
多行注释
/*
*/
变量定义:
name = 'tuchao' 全局变量定义
window.name = 'tuchao' 全局变量定义(推荐写法)
var name = 'tuchao' 局部变量定义
字符串常用方法和属性:
obj.trim() //字符串去空白
var name = ' tuchao '
name
" tuchao "
name.trim()
"tuchao"
obj.charAt(index) //字符串索引
1
2
3
4
5
6
7
8
9
|
var
name=
'tuchao'
name.charAt(0)
"t"
name.charAt(1)
"u"
name.charAt(2)
"c"
name.charAt(3)
"h"
|
obj.substring(start,end)
1
2
3
4
5
6
|
var
name=
'tuchao'
name.substring(0,5)
"tucha"
name.substring(0,6)
"tuchao"
|
obj.indexOf(char) //通过字符串找索引
1
2
3
|
var
name =
'tuchao'
name.indexOf(
'u'
)
1
|
obj.length //输出字符串长度
数组:
声明,如:
var array
=
Array() 或 var array
=
[]
添加
obj.push(ele) 追加
obj.unshift(ele) 最前插入
obj.splice(index,
0
,
'content'
) 指定索引插入
移除
obj.pop() 数组尾部获取
obj.shift() 数组头部获取
obj.splice(index,count) 数组指定位置后count个字符
切片
obj.
slice
(start,end)
合并
newArray = obj1.concat(obj2)
翻转
obj.reverse()
字符串化
obj.join(
'_'
)
长度
obj.length
函数:
function Foo (name) {
}
example:
1
2
3
4
5
6
7
|
function
Foo (name) {
var
arg2 = arguments[1]
console.log(name);
console.log(arg2);
}
Foo(
'aaaaa'
,
'bbbbbb'
)
|
这里的var arg2 = arguments[1] 可以给函数追加参数
自执行函数:
(function(){
})()
example:
1
2
3
|
(
function
(name){
console.log(name);
})(
'arguments arg one'
)
|
执行结果:
for循环:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
var
array = [
'one'
,
'two'
,
'three'
,
'four'
,
'five'
,
'six'
]
//定义数组
var
dict = {
'name'
:
'tuchao'
,
'age'
:
'21'
,
'profession'
:
'computer'
}
//定义字典
for
(
var
item
in
array){
console.log(item)
}
0
1
2
3
4
5
注:这里默认循环的是数组的下标
for
(
var
i
in
dict) {
console.log(dict[i])
}
tuchao
21
computer
for
(
var
i=0;i<array.length;i++){
console.log(array[i])
}
one
two
three
four
five
six
|
本文转自qw87112 51CTO博客,原文链接:http://blog.51cto.com/tchuairen/1740066