`
yangdong
  • 浏览: 65188 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

firstChild 把我玩了

 
阅读更多
加班两三个小时,就是因为被一个 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 传进来?我认为这是这个函数的设计问题。
分享到:
评论
4 楼 yangdong 2008-08-13  
没错。所以现在即便别人没加我都加上。

to williamy:
DWR util 里面都是辅助操作DOM得好东西。他们的函数都解决了跨浏览器的问题。而且有些烦人的IE bug也能绕过去。我现在JS必用。没事也会看看里面的代码,很有用。YUI也有辅助函数,可惜没时间看。
3 楼 笨笨狗 2008-08-13  
你不加tbody,浏览器也会给你加上的,如果需要用js操作子元素,你不加的话就是自己给自己找麻烦,何况,这样也不遵循标准,是个坏习惯。
2 楼 williamy 2008-08-12  
原来DWR还能这么用,不过这么用有什么用?徒增烦恼
1 楼 supercrsky 2008-08-11  
tbody一般我也不加。

相关推荐

    firstchild-lastchild

    firstchild-lastchild

    解析dom中的children对象数组元素firstChild,lastChild的使用

    以下是对dom中的children对象数组元素firstChild,lastChild的使用进行了详细的分析介绍,需要的朋友可以参考下

    loadanswer

    XmlElement XStuname = (XmlElement)document.DocumentElement.FirstChild;//获取'学生姓名'节点 //richTextBox1 .Text = "学生姓名:" + XStuname.FirstChild.InnerText;//读取'学生姓名'节点的内容 XmlElement ...

    Web.xml文件配置JDOM對XML文件操作

    public Element ReaderXml(String fileName,String firstChild){ Element element=null; try{ File xmlFile = new File(fileName); SAXBuilder sAXBuilder=new SAXBuilder(false); Document document=...

    java基于链表实现树结构(算法源码)

    private TreeLinkedList parent, firstChild, nextSibling;//父亲、长子及最大的弟弟 //(单节点树)构造方法 public TreeLinkedList() { this(null, null, null, null); } //构造方法 public TreeLinkedList...

    ajax联动查询后绑定控件

    ajax 异步请求数据后返回绑定控件 //处理DOM对象 ... oOption.value = childs[0].firstChild.nodeValue; oOption.text = childs[1].firstChild.nodeValue; objSel_goodsDesc.add(oOption); } }

    javascript 学习之旅 (3)

    childType属性:返回的数组包含着所有类型的节点 nodeValue属性:改变某个文本节点的值 firstChild和lastChild属性:无论何时何地,只要需要访问childNodes[]数组的第一个元素,外面都可以把它写成firstChild。...

    FLASH+XML构造下拉菜单 fla源文件

    _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; } } //否则删除该...

    JS遍历DOM文档树的方法实例详解

    遍历文档树通过使用parentNode属性、firstChild属性、lastChild属性、previousSibling属性和nextSibling属性来实现。 1、parentNode属性 该属性返回当前节点的父节点。 [pNode=]obj[removed] pNode:该参数用来存储...

    JQery jstree 大数据量问题解决方法

    问题解决:生成的树是逐级加载的,在open函数中有一个生成节点的代码: 代码 代码如下: ... if ($(firstChild).attr(‘id’)==-1) TREE_OBJ.remove(firstChild); 问题就出 TREE_OBJ.create函数上,这个函数很消耗性能

    AJAX和DOM的运行经验

    被AJAX中DOM的操作郁闷了好几天,今天总算搞明白了,自学就是苦啊,苦的一把鼻涕一把泪的, 把教训些出来,给后来者提个醒,老鸟就不要看了。 1.DOM中的对XML的操作不要和浏览器中的弄混掉了,比如getElementByName...

    js常见的兼容问题.js

    js常见的兼容问题: ...4.关于使用 firstChild,lastChild 等,获取第一个/最后一个元素节点时的兼容; 5.添加监听时间addEventListerner/attachEvent 出现的兼容问题; 6.关于获取滚动条距离而出现的问题;

    soal-shift-sisop-modul-2-C07-2021

    firstchild = fork(); if (firstchild &lt; 0){ exit(EXIT_FAILURE); } if (firstchild == 0) { char *arg[] = {"mkdir",nmfolder[0],nmfolder[1],nmfolder[2],NULL}; execv("/bin/mkdir",arg); } 上面...

    js DOM学习笔记11

    如果父节点没有子节点, 则firstChild和lastChild都为nullparentNode 父节点previousSibling 第一个节点的previ

    java-mvp:这是带有被动视图的Java Swing MVP示例。 使用Spring Context作为依赖项注入框架实现

    具有被动Swing视图和&Spring作为IoC框架的HMVP 层次模型–视图–演示者? 这只是一个简单的项目,展示... 受到以下参考的启发:UML图像的:示例应用程序界面:MainFrame依赖项:FirstChild依赖项:SecondChild依赖项:

    初识javascript 文档碎片

    后来是一技术群问到以下一段代码: 代码如下: function html2node(s) { var d = ... while (d.firstChild) df.appendChild(d.firstChild); return df; } 大概的原理都明白,比较困惑的是为什么要用 document.cre

    JavaScript遍历DOM元素的常见方式示例

    对于元素之间的空格,IE9以及之前的版本不会返回文本节点,,其他的浏览器会返回文本节点,所以我们在使用firstChild,lastChild时会导致行为不一致。 DOM中为元素新增了下面几个属性: childElementCount:返回子...

    Bergsoft NextSuite v6.0.70 NOV 26 2017

    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 & ...

    DOMhelp.js

    var tempObj = node[removed].firstChild; while(tempObj.nodeType != 1 && tempObj.nextSibling != null){ tempObj = tempObj.nextSibling; } return (tempObj.nodeType == 1)?tempObj:false; },

Global site tag (gtag.js) - Google Analytics