Javascript Row.cells[0]。firstChild在IE和FF中返回不同的值。

Javascript Row.cells[0]。firstChild在IE和FF中返回不同的值。,javascript,jquery,internet-explorer,firefox,cross-browser,Javascript,Jquery,Internet Explorer,Firefox,Cross Browser,我有一个javascript函数(function toggleTree(theRow)),其中行从表onclick中的一个单元格传递给该函数 <tr class="rowWhite"><td style="text-align:center"><img class="expand" alt="" src="images/plus.jpg"onclick="toggleTree(this.parentNode.parentNode);" /> theRow

我有一个javascript函数(function toggleTree(theRow)),其中行从表onclick中的一个单元格传递给该函数

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

theRow.cells[0].firstChild.className和theRow.cells[0].firstChild.src的值在IE和Firefox中不同,因此该函数在FF中不起作用,但在IE中起作用。如何从任何浏览器将这些值输入到jsfunction中?

您可能在IE以外的其他浏览器中获得textnode

像这样的怎么样

window.onload=function(){
var expImg=document.queryselectoral(“img.expand”);//例如
console.log(expImg)

对于(var i=0;iYou必须有一些未显示的空白。还要缓存对象,并在srcDefine“different”中的“end”后面加一个空格?每个元素返回什么值?元素可能重复。firstChild返回“you want
。children[0]
而不是
.firstChild
,因为某些浏览器会将空白作为firstChild文本节点返回。queryselector很好,但在旧版IE中不起作用。请确保在使用它之前不需要支持IE6或IE7。
    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";
}
} 
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
        }
    }
}