获取js动态创建的元素

linpyi 2007-12-03 06:15:27
我抄了一个类似google查询的代码,想修改下,碰到一些问题
//返回函数
function processMatchResponse() {
if (XMLHttpReq.readyState == 4) {
if (XMLHttpReq.status == 200) {
setNames(XMLHttpReq.responseXML.getElementsByTagName("companyName"), XMLHttpReq.responseXML.getElementsByTagName("userId"));
} else {
alert("\u8fde\u63a5\u5931\u8d25");
}
}
}
//生成与输入内容匹配行
function setNames(names, ids) {
clearNames();
var size = names.length;
setOffsets();
var row, cell, txtNode;
var row1, cell1, txtNode1, cell2, txtNode2;
for (var i = 0; i < size; i++) {
var nextNode = names[i].firstChild.data;
var lastNode = ids[i].firstChild.data;
row = document.createElement("tr");
cell = document.createElement("td");
cell2 = document.createElement("td");
row.onmouseout = function () {
this.className = "mouseOver";
};
row.onmouseover = function () {
this.className = "mouseOut";
};
row.setAttribute("style", "cursor:hand");
row.setAttribute("bgcolor", "#FFFAFA");
row.setAttribute("border", "0");
txtNode = document.createTextNode(nextNode);//获取公司名
cell.appendChild(txtNode);
txtNode2 = document.createTextNode(lastNode);//获取公司ID
cell2.appendChild(txtNode2);
row.appendChild(cell);
row.appendChild(cell2);
cell.onclick = function () {
completeField(this);//填入文本框
};
completebody.appendChild(row);

// aa.style.display = "";
}
if (popup.scrollHeight > 150) {
popup.style.height = "115px";
popup.style.overflow = "auto";
} else {
popup.style.height = "";
popup.style.overflow = "";
}
//在最后生成一个关闭按钮
row1 = document.createElement("tr");
cell1 = document.createElement("td");
cell1.setAttribute("style", "cursor:hand");
cell1.setAttribute("bgcolor", "#FFFAFA");
cell1.setAttribute("border", "0");
cell1.onclick = function () {
closeDiv(this);
};
txtNode1 = document.createTextNode("\u5173\u95ed");
cell1.appendChild(txtNode1);
row1.appendChild(cell1);
completebody.appendChild(row1);
}
//清除自动完成行
function clearNames() {
var ind = completebody.childNodes.length;
for (var i = ind - 1; i >= 0; i--) {
completebody.removeChild(completebody.childNodes[i]);
}
complete_body.style.border = "none";
if (aa.style.display != "none") {//判断关闭
aa.style.display = "none";
}
}

//填写输入框
function completeField(row) {
alert(row.firstChild.nodeValue);
// inputField.value = cell.firstChild.nodeValue;
// document.getElementById("ci_UserID").value = userid;
// var cc_UserNameID = inputField.value;
// document.getElementById("cc_UserNameID").readOnly = true;
// document.getElementById("isCheck").value = "1";
// isOK.innerHTML = "√\u53ef\u4ee5\u4f7f\u7528";
// popup.style.display = "none";
// clearNames();
}
我想把从后台取到的值填入text中,动态创建了一个completeField的js函数,如果是把单条信息放入text
cell.onclick = function () {
completeField(this);//填入文本框
};
function completeField(cell){
alert(cell.firstChild.nodeValue);
}
可以获取到值
我现在想把cell和cell2一起放入row
在写个row.onclick = function () {
completeField(this);//填入文本框
};
我要怎么获取row中cell和cell2的节点的值


我如果写成row.onclick = function () {
completeField(cell);//填入文本框
};
这个cell始终都是后台传过来的最后的一个值,大家帮忙下
...全文
330 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
linpyicool 2007-12-04
  • 打赏
  • 举报
回复
又得自己结贴,搞了一天终于搞出来了
cell2.appendChild(txtNode2);
row.appendChild(cell);
row.appendChild(cell2);
cell.onclick = function () {
completeField(this);//填入文本框
};
这样可以获取一列的JS事件
row.onclick = function () {
completeField(this);//填入文本框
};
function completeField(row) {
alert(row.cells[0].firstChild.nodeValue);//可以使用 row.cells[0].来获取第N列的值

}
linpyi 2007-12-03
  • 打赏
  • 举报
回复
cell.onclick = function () {
completeField(cell);
};
是获取到,i循环的最后一个值,也就是最后一个节点
擒兽 2007-12-03
  • 打赏
  • 举报
回复
cell.onclick = function () {
completeField(cell);//这样试试
};

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧