函数:push(para),参数说明para要添加的元素
示例:
var lang = ["php","java","javascript"]
lang.push('.net')
结果没团:lang = ["php","java","javascript",".net"]
2、删除和替换元素
函数:splice(para1,para2,para3),参数说明para1其实悄察判位置(从0开始),para2要删除的元素个数,para3(可选)要替换的元素
示例:
a、删除
var lang = ["php","java","javascript"]
lang.splice(0,1)
结果:lang = ["java"启改,"javascript",".net"]
b、替换
var lang = ["php","java","javascript"]
lang.splice(0,1,'.net')
结果:lang = [".net","javascript",".net"]
window.onload = function () {const params = document.getElementById('params')
const ul = params.querySelector('ul')
const addBtn = ul.querySelector('#add')
addBtn.onclick = function (e) {
const newli = document.createElement('li')
newli.appendChild(document.createElement('input'))
const btn = document.createElement('input')
btn.setAttribute('type', 'button')
btn.setAttribute('value', '删除')
btn.setAttribute('opt-type', 'del')
穗搜纤 newli.appendChild(btn)
ul.appendChild(newli)
}
ul.onclick = function (e) {
const target = e.target
const optType = target.getAttribute('猜仿opt-type')
if (target.nodeName === 'INPUT' && target.type === 'button' && optType === 'del') {
this.removeChild(target.parentNode)
}
}
} <div id="params">
<ul>
<li><input type="text"><input id="add" type="button" 漏埋value="添加一行"></li>
</ul>
</div>
点击"添加一行",生成一条li,包含input,然后append进ul里边。点击删除,用ul删除按钮父元素li。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)