Firefox中 空白字符,比如回车,空格等也算作一个Node
就是firstChild,nextsbiling这两个.
下面给出函数吧.还是代码比较说明问题
代码都是网上来的.
不过要注意的是,getNext和getFirstChild是不一样的
next是下一个,同级别的下一个,不会取到自己这个节点的子节点.
可能是因为对dom的理解不一样ie和firefox对firstChild,nextSbiling的处理不太一样.
所以要取到下一个结点,只能用type来判断了.
function getNextSibling(startBrother){
endBrother=startBrother.nextSibling;
while(endBrother.nodeType!=1){
endBrother = endBrother.nextSibling;
}
return endBrother;
}
function getNextSibling1(obj){
if(obj.nextSibling.nodeType==3) {
sibling=obj.nextSibling.nextSibling; // Moz. Opera
}
else {
sibling=obj.nextSibling; // IE
}
return sibling;
}
function getFirstChild(obj){
for (i=0; i<obj.childNodes.length; i++){
if (obj.childNodes[i].nodeType==1)
return obj.childNodes[i];
else
continue;
}
需要使用的时候先getElementByid获取到首结点,然后就可以取到后面的结点了.
另外就是ie不区分变量大小写,收到html首先就格式化了,所以要注意id可能会相同.
会导致一些问题.
另外就是ie会把NULL字符转换成space空格,firefox是不会处理的,但是
如果javascript里面处理NULL会有一些问题.opera基本能够兼容IE的一些
函数,比如documnet.all等等.基本都能用吧,不过空字符还是不行的.是变量中的空字符
会带来问题.
写了个例子
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS控制HTMLDOM表格行上下移动</title>
<style type="text/css">
<!--
td { text-align:center;font-size:12px;padding:3px;}
.tdwidth {
width: 30px;
text-align: center;
}
-->
</style>
</head>
<body>
<table id="table1" bordercolor="#000000" width="300" border="1">
<tr onclick="setTrProperty(this)" id="tr1">
<td width="25%">1</td>
<td width="25%">11</td>
<!--使用javascript:void(0)是为了能够传递this参数到事件处理程序-->
<td width="25%"><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td width="25%"><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
<tr onclick="setTrProperty(this)" id="tr2">
<td>2</td>
<td>22</td>
<td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
<tr onclick="setTrProperty(this)" id="tr3">
<td>3</td>
<td>33</td>
<td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
<tr onclick="setTrProperty(this)" id="tr4">
<td>4</td>
<td>44</td>
<td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
<tr onclick="setTrProperty(this)" id="tr5">
<td>5</td>
<td>55</td>
<td><a href="javascript:void(0)" onclick="moveUp(this)">上移</a></td>
<td><a href="javascript:void(0)" onclick="moveDown(this)">下移</a></td>
</tr>
</table>
<table id="table2" bordercolor="#000000" width="300" border="0">
<tr><td height="10"></td></tr>
<tr>
<td align="center">
<input type="button" class="tdwidth" value=" ̄" onclick="toUp()"/>
<input type="button" class="tdwidth" value="↑" onclick="moveUp()"/>
<input type="button" class="tdwidth" value="↓" onclick="moveDown()"/>
<input type="button" class="tdwidth" value="_" onclick="toDown()"/>
</td>
<td>
<input type="button" name="btn_save" value="保存" onclick="saveValue()"/>
</td>
</tr>
</table>
</body>
</html>
<script language="JavaScript" type="text/javascript">
<!--
var _obj;
function setTrProperty(_e){
if(_obj && _obj.style){
_obj.style.background = "";
}
_obj = _e;
_obj.style.background = "#F7F709";
}
function toUp(){
while(_obj && getPreviousSibling(_obj)!=null){
swapNode(_obj,getPreviousSibling(_obj));
}
}
function toDown(){
while(_obj && getNextSibling(_obj)!=null){
swapNode(_obj,getNextSibling(_obj));
}
}
function moveDown(){
//if it's not the end row ,swap with the next row
if(_obj && getNextSibling(_obj))swapNode(_obj,getNextSibling(_obj));
}
function moveUp(){
//if it's not the first row ,swap with the previous row
if(_obj && getPreviousSibling(_obj))swapNode(_obj,getPreviousSibling(_obj));
}
//define swap
function swapNode(node1,node2){
//get the parentNode
var _parent=node1.parentNode;
//get their nextNode
var _t1=getNextSibling(node1);
var _t2=getNextSibling(node2);
//insert node2 into the position of the node1
if(_t1){
_parent.insertBefore(node2,_t1);
}else{
_parent.appendChild(node2);
}
//insert node1 into the position of the node2
if(_t2){
_parent.insertBefore(node1,_t2);
}else{
_parent.appendChild(node1);
}
}
function getNextSibling(obj){
if(obj && obj.nextSibling){
if(obj.nextSibling.nodeType==3) {
while(obj.nextSibling.id==undefined){
obj = obj.nextSibling;
sibling = obj.nextSibling;// Moz. Opera .FF
if(sibling==null){
break;
}
}
//sibling=obj.nextSibling.nextSibling; // Moz. Opera .FF
}else {
sibling=obj.nextSibling; // IE
}
return sibling;
}else{
return null;
}
}
function getPreviousSibling(obj){
if(obj && obj.previousSibling){
if(obj.previousSibling.nodeType==3) {
while(obj.previousSibling.id==undefined){
obj = obj.previousSibling;
sibling = obj.previousSibling;// Moz. Opera .FF
if(sibling==null){
break;
}
}
}
else {
sibling=obj.previousSibling; // IE
}
return sibling;
}else{
return null;
}
}
//-->
</script>