本文介绍了如何动态创建div中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{for (var i = 0; i < obj.length - 1; i++) {
var rwdata = obj[i].split("|");
document.getElementById('dvMainChatbox').style.display = "";
var MainDiv = document.getElementById('dvMainChatbox');
var ChildDiv = document.createElement('DIV');
ChildDiv.setAttribute('id', 'div_' + rwdata[0] + '_' + rwdata[1]
//ChildDiv.innerHTML = rwdata[1];
// ChildDiv.setAttribute("label", "id");
ChildDiv[i] = document.createElement('label');
//ChildDiv[i] = rwdata[i].innerText;
rwdata[i] = document.createTextNode(obj[i]);
ChildDiv[i].appendChild(rwdata[i]);
document.getElementById('DIV' + i).appendChild(obj[i]);
}
推荐答案
<script type="text/javascript">
function addElement() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value - 1) + 2;
numi.value = num;
var newdiv = document.createElement('label');
var divIdName = 'my' + num + 'label';
newdiv.setAttribute('id', divIdName);
newdiv.innerHTML = "Element Number ' + num + ' has been added with id = '"+divIdName+"'!";
ni.appendChild(newdiv);
}
</script>
</head>
<body>
<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>
</body>
</html>
这篇关于如何动态创建div中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!