2013-10-02 49 views
0

我有一個JavaScript函數(函數toggleTree(theRow)),其中行從表中的單元格傳遞給此函數onclick。Row.cells [0] .firstChild在IE和FF中返回不同的值。

<tr class="rowWhite"><td style="text-align:center"><img class="expand" alt="" src="images/plus.jpg"onclick="toggleTree(this.parentNode.parentNode);" /> 

切換樹狀函數如下。

function toggleTree(theRow) 
{ 
var imgEl = theRow.cells[0].firstChild; 
if (theRow.cells[0].firstChild.className == "expand") 
{ 
theRow.cells[0].firstChild.className="collapse"; 
theRow.cells[0].firstChild.src="images/minus.jpg"; 
alert(theRow.cells[0].firstChild.className); 
alert(theRow.cells[0].firstChild.src); 
return "expand"; 
} 
else 
{ 
theRow.cells[0].firstChild.className="expand"; 
theRow.cells[0].firstChild.src="images/plus.jpg"; 
alert(theRow.cells[0].firstChild.className); 
alert(theRow.cells[0].firstChild.src); 
return "collapse"; 
} 
} 

theRow.cells的值[0] .firstChild.className和theRow.cells [0] .firstChild.src是在IE和Firefox不同,由於其,該函數不會在FF工作,但它在IE中工作。我如何從任何瀏覽器中將值傳入jsfunction?

+2

你必須有一些空白你不顯示。也緩存你的對象,並在結束後放置一個空間「在src – mplungjan

+0

定義」不同「?每個返回什麼值? –

+0

可能重複[element.firstChild返回' mplungjan

回答

0

你很可能得到一個textnode在其他瀏覽器比IE

如何像

Live Demo

window.onload=function() { 
    var expImg = document.querySelectorAll("img.expand"); // for example 
    console.log(expImg) 
    for (var i=0;i<expImg.length;i++) { 
     expImg[i].onclick=function() { 
      var exp = this.className == "expand"; 
      this.className=exp?"collapse":"expand"; 
      this.setAttribute("alt",this.className); 
      this.src=exp?"images/minus.jpg":"images/plus.jpg"; 
      // here you can do something with the row 
     } 
    } 
} 
+0

querySelectorAll很棒,但它在傳統IE中不起作用,請確保在使用之前不需要支持IE6或IE7 – rescuecreative

+0

請問downvoter解釋投票? – mplungjan

相關問題