js动态添加、删除html代码

时间:2024-03-05 11:18:07
删除
例1:
function AddOrDelModel(modelId){
var insertText = \'<table tag=\' + modelId + \'><tr><td>\' + modelId + \'被选中</td></tr></table>\', insertEl = document.getElementById("insert");
if(document.getElementById(modelId).checked){
insertEl.innerHTML = insertEl.innerHTML + insertText;
}else{
var tables = insertEl.getElementsByTagName("table");
for(var i = 0; i < tables.length; i++){
if(tables[i].tag = modelId){
insertEl.removeChild(tables[i]);
break;
}
}
}
}
例2:

function Cross(id) {

var obj = document.getElementById("ChannelId" + id);//建议使用ID
if (obj != null) {
obj.parentNode.removeChild(obj);
}
}

 添加:

例:

function add() {
var id = document.getElementsByTagName("input").length;//获取input控件个数
var dv = document.getElementById(\'ChannelId1\');//获取指定控件

dv.outerHTML = dv.outerHTML
+ \'<li id="ChannelId\' + id + \'"><span class="left"><label class="fldTitle">名称</label><div class="fieldwrap input-uniform"><input  class="large textips" type="text" style="width: 362px;" /><a class="right" href="#" onclick="Cross(\' + id + \')"><span class="icon-block-color cross-c" style="margin-top: 4px;margin-left: 5px;" title="删除控件"></span></a></div></span></li>\';
}