动态的创建客户端控件[收藏网络]

动态的操作客户端控件,现在流行的有2种方法,
一是innerHTML,这种方法无可厚非,一样可以完成任务,但我不推荐这种方法,
二是window.document.createElement(),

例如:在web中有这样一段标记 <div id="TestDIv"></div>

我们可以这样在DIV中创建一个table:

var table = window.document.createElement("table");

var row1 = window.document.createElement("tr");

var cell1 = window.document.createElement("td");

var cell1_value = window.document.createTextNode("A test table!");

cell1.appendChild(cell1_value);

row1.appendChild(cell1);

table.appendChild(row1);

window.document.getElementById("TestDiv").appendChild(table);

同理,创建其他控件的时候,只需要注意要创建的标记就可以了,比如要创建select的时候,他就要append option了

在修改的时候,要先wnidow.document.getElementById,然后再搜索要更改的节点,

1. childNodes[x] , 根据序号定位

2.nextSibling, 当前节点的下个节点

3.previousSibling 当前节点的上个节点

4. firstChild

5. lastChild
其实用上面的方法也可以操作服务端的一些控件,因为从服务端发送到客服端,在客户端展现的时候,是以最基本的HTML来展现的

原文地址:https://www.cnblogs.com/hejunrex/p/1652071.html