文章目录
- 1.在html中初始化js
- 2.变量
- 3.字符串
- 4.数组
- 5.对象
1.在html中初始化js
<div class="menus">
<div class="header" onclick="myFunc()">标题</div>
<div class="item">内容</div>
</div>
<script src="static/my.js"></script>
<script type="text/javascript">
function myFunc() {
alert("Hello!");
confirm("Continue?");
}
</script>
2.变量
var name = "xie";
console.log(name);
3.字符串
var name = "xie";
var name = String("xie");
var name = "xie";
var v1 = name.length;
var v2 = name[0];
var v3 = name.trim();
var v4 = name.substring(0, 2);
4.数组
var v1 = [1, 2, 3, 4];
var v2 = Array([1, 2, 3, 4]);
v1[0] = 123;
v1.push("abc");
v1.unshift("123");
v1.splice(1, 0, "123");
v1.pop();
v1.shift();
v1.splice(2, 1);
for(var idx in v1){
console.log(v1[idx]);
}
for(var i = 0; i < v1.length; i++){
console.log(v1[i]);
}
5.对象
info = {
name: "xie",
age: 18
}
info.name = "yang";
info["age"] = 20;
delete info["age"]
for(var key in info){
value = info[key]
}