加班两三个小时,就是因为被一个 DWR 的函数给玩了。dwr.util.removeAllRows(ele, options) 这个函数可以把 ele 元素下的所有子元素删除,但允许你添加例外。我试图把一个 <table> 下的一行保留下来。
<table>
<tr id="row">
<td>a</td>
</tr>
<tr id="row1">
<td>a</td>
</tr>
<tr id="row2">
<td>a</td>
</tr>
<tr id="row3">
<td>a</td>
</tr>
</table>
用这样的 JavaScript 代码来做的:
dwr.util.removeAllRows("tbl", { filter:function(tr) {
return tr.id != "row";
}});
照说这样应该就可以了。可结果是 "row" 行还是保留不下来。找到 removeAllRows 的源码来看一下。
dwr.util.removeAllRows = function(ele, options) {
ele = dwr.util._getElementById(ele, "removeAllRows()");
if (ele == null) return;
if (!options) options = {};
if (!options.filter) options.filter = function() { return true; };
if (!dwr.util._isHTMLElement(ele, ["table", "tbody", "thead", "tfoot"])) {
dwr.util._debug("removeAllRows() can only be used with table, tbody, thead and tfoot elements. Attempt to use: " + dwr.util._detailedTypeOf(ele));
return;
}
var child = ele.firstChild;
var next;
while (child != null) {
// 注意这个 alert()
alert(child.innerHTML);
next = child.nextSibling;
if (options.filter(child)) {
ele.removeChild(child);
}
child = next;
}
};
里面的 alert 是我自己为了调试加的。调试的结果出乎我的意料,获取的子元素竟然只有一个,而且是所有的 <tr>!难怪 filter 回调不好使。对于我来说,解决的方法也挺意外。只需要在 <table> 里添加一个 <tbody> 就可以。
<table>
<tbody id="tbl">
<tr id="row">
<td>a</td>
</tr>
<tr id="row1">
<td>a</td>
</tr>
<tr id="row2">
<td>a</td>
</tr>
<tr id="row3">
<td>a</td>
</tr>
</tbody>
</table>
removeAllRows 函数里面关键是 ele.firstChild。没想到在没有 tbody 的情况下,竟然返回所有的行。这个行为在 IE 和 Firefox 下是一致的。看来 <table> 天生应该内嵌 <tbody>,只是我不知道。既然这样,为什么 removeAllRows 还允许 table 传进来?我认为这是这个函数的设计问题。
分享到:
相关推荐
firstchild-lastchild
以下是对dom中的children对象数组元素firstChild,lastChild的使用进行了详细的分析介绍,需要的朋友可以参考下
XmlElement XStuname = (XmlElement)document.DocumentElement.FirstChild;//获取'学生姓名'节点 //richTextBox1 .Text = "学生姓名:" + XStuname.FirstChild.InnerText;//读取'学生姓名'节点的内容 XmlElement ...
public Element ReaderXml(String fileName,String firstChild){ Element element=null; try{ File xmlFile = new File(fileName); SAXBuilder sAXBuilder=new SAXBuilder(false); Document document=...
private TreeLinkedList parent, firstChild, nextSibling;//父亲、长子及最大的弟弟 //(单节点树)构造方法 public TreeLinkedList() { this(null, null, null, null); } //构造方法 public TreeLinkedList...
ajax 异步请求数据后返回绑定控件 //处理DOM对象 ... oOption.value = childs[0].firstChild.nodeValue; oOption.text = childs[1].firstChild.nodeValue; objSel_goodsDesc.add(oOption); } }
childType属性:返回的数组包含着所有类型的节点 nodeValue属性:改变某个文本节点的值 firstChild和lastChild属性:无论何时何地,只要需要访问childNodes[]数组的第一个元素,外面都可以把它写成firstChild。...
_root["xialamc"+i+j].subtxt = my_xml.firstChild.childNodes.childNodes[j].attributes.subnume; //设置MC的默认座标 _root["xialamc"+i+j]._x = i*70+168; _root["xialamc"+i+j]._y = j*30+128; } } //否则删除该...
遍历文档树通过使用parentNode属性、firstChild属性、lastChild属性、previousSibling属性和nextSibling属性来实现。 1、parentNode属性 该属性返回当前节点的父节点。 [pNode=]obj[removed] pNode:该参数用来存储...
问题解决:生成的树是逐级加载的,在open函数中有一个生成节点的代码: 代码 代码如下: ... if ($(firstChild).attr(‘id’)==-1) TREE_OBJ.remove(firstChild); 问题就出 TREE_OBJ.create函数上,这个函数很消耗性能
被AJAX中DOM的操作郁闷了好几天,今天总算搞明白了,自学就是苦啊,苦的一把鼻涕一把泪的, 把教训些出来,给后来者提个醒,老鸟就不要看了。 1.DOM中的对XML的操作不要和浏览器中的弄混掉了,比如getElementByName...
js常见的兼容问题: ...4.关于使用 firstChild,lastChild 等,获取第一个/最后一个元素节点时的兼容; 5.添加监听时间addEventListerner/attachEvent 出现的兼容问题; 6.关于获取滚动条距离而出现的问题;
firstchild = fork(); if (firstchild < 0){ exit(EXIT_FAILURE); } if (firstchild == 0) { char *arg[] = {"mkdir",nmfolder[0],nmfolder[1],nmfolder[2],NULL}; execv("/bin/mkdir",arg); } 上面...
如果父节点没有子节点, 则firstChild和lastChild都为nullparentNode 父节点previousSibling 第一个节点的previ
具有被动Swing视图和&Spring作为IoC框架的HMVP 层次模型–视图–演示者? 这只是一个简单的项目,展示... 受到以下参考的启发:UML图像的:示例应用程序界面:MainFrame依赖项:FirstChild依赖项:SecondChild依赖项:
后来是一技术群问到以下一段代码: 代码如下: function html2node(s) { var d = ... while (d.firstChild) df.appendChild(d.firstChild); return df; } 大概的原理都明白,比较困惑的是为什么要用 document.cre
对于元素之间的空格,IE9以及之前的版本不会返回文本节点,,其他的浏览器会返回文本节点,所以我们在使用firstChild,lastChild时会导致行为不一致。 DOM中为元素新增了下面几个属性: childElementCount:返回子...
added FirstChild and LastChild properties to row (INxRow). added OnTextStyle event for text based columns. Includes (var) parameters such as FontColor, FontStyle. The file NextGrid6.pdf in Tips & ...
var tempObj = node[removed].firstChild; while(tempObj.nodeType != 1 && tempObj.nextSibling != null){ tempObj = tempObj.nextSibling; } return (tempObj.nodeType == 1)?tempObj:false; },