读书人

简单的节点有关问题求教

发布时间: 2012-02-03 22:02:47 作者: rapoo

简单的节点问题求教
<html>
<head>
<title> Table Sort Example </title>
<script type= "text/javascript ">

function compareTRs(oTR1, oTR2) {
var sValue1 = oTR1.cells[0].firstChild.nodeValue;
var sValue3 = oTR1.cells[0].innerText;
var sValue4 = oTR1.cells[0].firstChild.innerText;
alert(sValue1);
alert(sValue4);
var sValue2 = oTR2.cells[0].firstChild.nodeValue;

return sValue1.localeCompare(sValue2);
}


function sortTable(sTableID) {
var oTable = document.getElementById(sTableID);
var oTBody = oTable.tBodies[0];
var colDataRows = oTBody.rows;
var aTRs = new Array;

for (var i=0; i < colDataRows.length; i++) {
aTRs[i] = colDataRows[i];
}

aTRs.sort(compareTRs);

var oFragment = document.createDocumentFragment();
for (var i=0; i < aTRs.length; i++) {


oFragment.appendChild(aTRs[i]);
}

oTBody.appendChild(oFragment);
}


</script>
</head>

<body>
<p> Click on the table header to sort in ascending order. </p>
<table border= "1 " id= "tblSort ">
<thead>
<tr>
<th onclick= "sortTable( 'tblSort ') " style= "cursor:pointer "> Last Name </th>
</tr>
</thead>
<tbody>
<tr>
<td> Smith </td>
</tr>
<tr>
<td> Johnson </td>
</tr>
<tr>
<td> Henderson </td>
</tr>
<tr>
<td> Williams </td>
</tr>


<tr>
<td> Gilliam </td>
</tr>
<tr>
<td> Walker </td>
</tr>
</tbody>
</table>
</body>
</html>


var sValue1 = oTR1.cells[0].firstChild.nodeValue;
var sValue3 = oTR1.cells[0].innerText;
var sValue4 = oTR1.cells[0].firstChild.innerText;
这里为什么sValue1, sValue3 有直,但sValue4就为空呢?有什么区别吗?谢谢


[解决办法]
你试一下这个或许就明白了

for(var i in sValue4)
{
alert(i);
}
[解决办法]
没有该属性...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<!-- DW6 -->
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=utf-8 " />
<title> shawl.qiu template </title>
<style type= "text/css ">
/* <![CDATA[ */

/* ]]> */
</style>
<script type= "text/javascript ">
// <![CDATA[
if (navigator.appName== "Microsoft Internet Explorer ") {
//最大化窗口
self.moveTo(-5,-5)
self.resizeTo(screen.availWidth +8,screen.availHeight+8)
//这个脚本定义的宽度其实比原窗口还要大那么一点.
}
//]]>
</script>
<script type= "text/javascript ">
// <![CDATA[
function fListMtd(obj, e){
if(!e)var e=window.event;
var sur=e.srcElement||e.target;
try{w.close();}catch(e){}
w=open( ' ', 'newwin ', 'width=500,height=500,left=300,top=100,scrollbars ');
w.document.write( ' <meta http-equiv= "Content-Type " content= "text/html; charset=utf-8 " /> ');
w.document.write( ' <style> body{width:400px;word-w.document.writeap: break-word;} </style> ');
w.focus;
w.document.ondblclick=function(){w.close();}
fListMtd(obj, w);
w.onload=function(){ w.document.title=sur.firstChild.data;};
w.document.close();


return true;
function fListMtd(obj, target){
var ar=new Array();
var j=0;

for(var i in obj){
try{
ar[j]=i.bold()+ ' '+(typeof obj[i]).fontcolor( 'red ')+
' '+(obj[i]+ ' ').fontcolor( 'blue ');
j++;
}catch(e){
ar[j]=i.bold()+ ' '+(typeof obj[i]).fontcolor( 'red ')+
( ' empty ').fontcolor( 'red ').bold()
j++;
}
}
ar=ar.sort();
for(var i=0; i <ar.length; i++){
target.document.write(i+1, '. ',ar[i], ' <br/> ');
}
return true;
}
} // end function fListMtd

onload =
function()
{
fListMtd(document.getElementById( 'a ').firstChild, event);
}
//]]>
</script>
</head>
<body>
<div id= "a "> aaa </div>
</body>
</html>

[解决办法]

var sValue4 = oTR1.cells[0].firstChild;
for(var i in sValue4)
{
alert(i);
}
可以看出没有innerText这个属性,也就是说你得到的这个firstChild已经不是一个XML/HTML Element
[解决办法]
晕, 你用点书啃啃书吧...

nodeType 不同, 属性也不会相同....

读书人网 >JavaScript

热点推荐