var o = [2,3,5]
o[3] = 7
console.log(o)//[2,3,5,7]
除了这种方法,还可以通过使用push()语句来达到相同的目的:
o.push(11)
console.log(o)//[2,3,5,7,11]
o.push(13,17)
console.log(o)//[2,3,5,7,11,13,17]
如果需要在数组开头添加新的成员,可以使用unshift()语句:
o.unshift(2014)
console.log(o)//[2014,2,3,5,7,11,13,17]
o.unshift(2013, 2012)
console.log(o)//[2013,2012,2014, 2,3,5,7,11,13,17]
与push()对应,如果需要从数组末尾删除一个成员,可以使用pop()语句,pop()语句将返回这个被删除的成员,而数组长度将减少1:
var p = o.pop()
console.log(p)//17
console.log(o.length)//9
与unshift()对应,如果需要从数组开头删除一个成员,可以使用shift()语句,shift()语句将返回这个被删除的成员,而数组长度将减少1:
var s = o.shift()
console.log(s)//2013
console.log(o.length)//8
除了shift()语句和pop()语句,还可以通过delete *** 作符来删除数组中的成员。与shift()和pop()不同的是,delete *** 作后数组的length属性将保持不变,也即数组将变得不连续。
JavaScript中还可以通过设定数组的length属性来对数组进行修改:当length值小于数组成员数时,JavaScript将对数组进行截取;当length值大于数组成员数时,JavaScript会将数组变得不连续。如果length值只读,那么在数组中直接定义新成员的 *** 作将会失败:
console.log(o)//[2012,2014, 2,3,5,7,11,13]
o.length = 2
console.log(o)//[2012,2014]
o.length = 4
console.log(o)//[2012,2014,undefined,undefined]
var a = [1,2,3]
Object.defineProperty(a, "length", {writable:false})
a[3] = 4
console.log(a)//[1,2,3]
直接给个例子吧
包含两个文件(index.jsp 和 index.js)<%@ page language="java" contentType="text/html charset=UTF-8"
pageEncoding="UTF-8"%>
</head>
<body onload="loadUserDatas()">
<div class="container">
<table class="table" id="table">
<caption><h2>迈睿练习一</h2></caption>
<caption>
<button type="button" class="btn btn-info" id="user_add" data-toggle="modal"
data-target="#myModal" onclick="optionUserData(this)">新增</button>
<button type="button" class="btn btn-info" id="user_delete" onclick="optionUserData(this)">删除</button>
<button type="button" class="btn btn-info" id="user_edit" data-toggle="modal"
data-target="#myModal" onclick="optionUserData(this)">编辑</button>
<button type="button" class="btn btn-info" id="user_find" onclick="optionUserData(this)">查询</button>
<input type="text" id="s_code" placeholder="按工号查询" style="width: 80px">
<input type="text" id="s_userName" placeholder="按姓名查询" style="width: 80px">
<input type="text" id="s_all" placeholder="全文搜索" style="width: 80px">
</caption>
<thead>
<tr>
<th>序号</th>
<th>工号</th>
<th>姓名</th>
<th>性别</th>
<th>密码</th>
<th>年龄</th>
<th>出生日期</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<!-- 模态框(Modal) -->
<div class="modal hide" id="myModal" role="dialog" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
&times
</button>
<h4 class="modal-title" id="myModalLabel">
新增用户
</h4>
</div>
<div class="modal-body" id="modal-body">
<label for="name">工号:</label>
<input type="text" class="form-control" id="m_code" placeholder="请输入工号">
<label for="name">姓名:</label>
<input type="text" class="form-control" id="m_userName" placeholder="请输入姓名">
<label for="name">性别:</label>
<input type="text" class="form-control" id="m_sex" placeholder="请输入性别">
<label for="name">密码:</label>
<input type="text" class="form-control" id="m_passWord" placeholder="请输入密码">
<label for="name">年龄:</label>
<input type="text" class="form-control" id="m_age" placeholder="请输入年龄">
<label for="name">出生日期:</label>
<input type="text" class="form-control" id="m_birthday" placeholder="请输入出生日期">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">保存
</button>
<button type="button" class="btn btn-primary">提交更改</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal -->
</div>
<script type="text/javascript" src="resource/jquery/jquery.js"></script>
<script type="text/javascript" src="resource/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
复制代码
复制代码
//存放所有用户
var users = users || {}
// *** 作类型
var operateType = ""
//存放搜索对象
var searchUsers = searchUsers || {}
//用户构造方法
var User = {
Create:function(code,userName,sex,passWord,age,birthday){
this.code = code
this.userName = userName
this.sex = sex
this.passWord = passWord
this.age = age
this.birthday = birthday
},
//添加用户
addUserData:function(){
if(this.code != ""){
users[this.code] = this
}
},
//删除用户
deleteUserData:function (){
for(var i in users){
if(this.code == users[i].code){
delete users[i]
}
}
},
//编辑用户
editUserData:function(){
for(var i in users){
if(this.code == users[i].code){
users[i].userName = this.userName
users[i].sex = this.sex
users[i].passWord = this.passWord
users[i].birthday = this.birthday
users[i].age = this.age
}
}
},
//查找用户
findUserData:function(data){
for(var i in users){
if(data.code.indexOf(users[i].code) >= 0 ||
data.userName.indexOf(users[i].userName) >= 0){
searchUsers[users[i].code] = users[i]
refreshDatas(searchUsers)
}
}
}
}
function New(aClass,aParams){
function new_(){
aClass.Create.apply(this,aParams)
}
new_.prototype = aClass
return new new_()
}
//bootstrap模态框事件
$('#myModal').on('hide.bs.modal', function () {
// 执行一些动作...
var inputElements = this.getElementsByTagName("input")
var userArr = []
for(var i=0i<inputElements.lengthi++){
userArr[i] = inputElements[i].value
}
var user = New(User,userArr)
//添加 *** 作
if(operateType == "add"){
user.addUserData()
refreshDatas(users)
//编辑 *** 作
}else if(operateType == "edit"){
user.editUserData()
refreshDatas(users)
}
})
/**
* 首次加载页面执行方法
*/
function loadUserDatas(){
var userArray = initUserDatas()
addRowData(userArray)
refreshDatas(users)
}
/**
* 初始化用户数据
*/
function initUserDatas(){
var initUser1 = New(User,["1001","小兰","女","1234","13","1991-1-1"])
var initUser2 = New(User,["1002","小毅","男","1234","13","1991-1-1"])
var initUser3 = New(User,["1003","兰花","女","1234","13","1991-1-1"])
var initUser4 = New(User,["1004","兰儿","女","1234","13","1991-1-1"])
users[initUser1.code] = initUser1
users[initUser2.code] = initUser2
users[initUser3.code] = initUser3
users[initUser4.code] = initUser4
return users
}
/**
* 往表格添加一行html数据
*/
function addRowData(datas){
var tbodyElement = document.getElementById("tbody")
var html = ""
var color = "warning"
var flag = true
for(var i in datas){
if(flag){
color = "info"
}else{
color = "warning"
}
html = html + "<tr class='"+ color +"'><td style='width:30px'><input type='checkbox'></td><td id='code'>"
+ datas[i].code +"</td><td id='userName'>"
+ datas[i].userName +"</td><td id='sex'>"
+ datas[i].sex +"</td><td id='passWord'>"
+ datas[i].passWord +"</td><td id='age'>"
+ datas[i].age +"</td><td id='birthday'>"
+ datas[i].birthday +"</td>"
+"</tr>"
flag = !flag//颜色转换
}
tbodyElement.innerHTML = html
}
/**
* 刷新用户数据
*/
function refreshDatas(datas){
addRowData(datas)
}
/**
* 收集一行数据
*/
function collectionRowData(param){
var tdElement = param.getElementsByTagName("td")
var userArr = []
for(var i=1i<tdElement.lengthi++){
var temp = tdElement[i].textContent
userArr[i-1] = temp
}
var user = New(User,userArr)
return user
}
/**
* 用户 *** 作方法
*/
function optionUserData(param){
//获得 *** 作类别
var optionType = param.getAttribute("id")
if(optionType == "user_add"){
operateType = "add"
}else if(optionType == "user_delete"){
var checkRowData = isCheckedData()
var user = collectionRowData(checkRowData)
user.deleteUserData()
refreshDatas(users)
}else if(optionType == "user_edit"){
operateType = "edit"
var checkRowData = isCheckedData()
var user = collectionRowData(checkRowData)
var modal_body = document.getElementById("modal-body")
var inputElements= modal_body.getElementsByTagName("input")
for(var i=0i<inputElements.lengthi++){
var temp = inputElements[i].id.substring(2,inputElements[i].id.length)
inputElements[i].value = user[temp]
}
}else if(optionType == "user_find"){
var s_code = document.getElementById("s_code").value
var s_userName = document.getElementById("s_userName").value
var s_all= document.getElementById("s_all").value
//搜索数据
var s_data = s_data || {}
s_data.code = s_code
s_data.userName = s_userName
s_data.all = s_all
var user = New(User,[])
user.findUserData(s_data)
}else{
}
}
/**
* 是否选中数据,返回选中数据的行
*/
function isCheckedData(){
var tbodyElement =document.getElementById("tbody")
var trElements = tbodyElement.getElementsByTagName("tr")
var flag = false
for(var i=0i<trElements.lengthi++){
var inputElement = trElements[i].getElementsByTagName("input")[0]
if(inputElement.checked){
flag = true
return trElements[i]
}
}
if(!flag){
alert("请选择一条记录!")
$('#myModal').unbind("on")
}
}
$(function(){$("btnAdd").click(function(){
var opt = $("selectList option:selected")
if(opt.value != "-1"){
var spn=$("<span>" + opt.text() + "</span>")
var delBtn = $("<a href="javascript:void(0)">删除</a>")
delBtn.click(function(){
spn.remove()
})
spn.append(delBtn)
$("body").append(spn)
}
})
})
var spn=$("<span>" + opt.text() + "</span>")这种生成后,继续引用的用法,我现在不确定是否有问题,毕竟已经两年没写代码了。就算给个思路吧。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)