0

テーブル onclick のセルからこの関数に行が渡される JavaScript 関数 (関数 toggleTree(theRow) ) があります。

<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 に取得するにはどうすればよいですか?

4

1 に答える 1

0

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
        }
    }
} 
于 2013-10-02T21:01:46.657 に答える