纯Javascript:onClick切换行/图像:Firefox / Chrome - 工作原理:IE - 不起作用

时间:2013-03-24 15:35:38

标签: javascript image internet-explorer google-chrome firefox

从Zeb Rawnsley(你摇滚!)获得了一些很好的帮助,帮助我解决上一个问题。

使用一些代码以允许通过图像后面的可折叠链接隐藏下级行。这在Firefox和Chrome中运行得非常好,但是在IE中的第一次迭代(IE 8,特别是我公司的标准)后,图像不会交替出现。

感兴趣的部分在这里(我认为):

var closedImgHTML = "<img name=\"togglepicture\" src=\"http://www.iconlooker.com/user-content/uploads/wall/thumb/misc._icons_closed_folder.png\" border=\"0\" height=\"50\">";

var openImgHTML = "<img name=\"togglepicture\" src=\"http://www.iconlooker.com/user-content/uploads/wall/thumb/misc._icons_open_folder.png\" border=\"0\" height=\"50\">";

可能在这里(但我不这么认为):

lnk.innerHTML =(lnk.innerHTML == openImgHTML)?closedImgHTML:openImgHTML;

此CodePen适用于工作版本(Firefox / Chrome):

http://codepen.io/anon/pen/yjLvh

这是工作版的HTML:

<html>
<head>
<style type="text/css">
table { empty-cells: show; }
cell {font-family:'Calibri';font-size:11.0pt;color: #000000;}
TD{font-family: Calibri; font-size: 10.5pt;}
TH{font-family: Calibri; font-size: 10.5pt; }
</style>

</head>
<body>
<SCRIPT type=text/javascript> 
var tbl;
var toggleimage=new Array("http://www.iconlooker.com/user-content/uploads/wall/thumb/misc._icons_open_folder.png","http://www.iconlooker.com/user-content/uploads/wall/thumb/misc._icons_closed_folder.png")

  var closedImgHTML = "<img name=\"togglepicture\" src=\"http://www.iconlooker.com/user-content/uploads/wall/thumb/misc._icons_closed_folder.png\" border=\"0\" height=\"50\">";

  var openImgHTML = "<img name=\"togglepicture\" src=\"http://www.iconlooker.com/user-content/uploads/wall/thumb/misc._icons_open_folder.png\" border=\"0\" height=\"50\">";

function trim(str){
 return str.replace(/^\s*|\s*$/g,"");
}

function getParent(el, pTagName) {
 if (el == null) return null;
 else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase
  return el;
 else
  return getParent(el.parentNode, pTagName);
}

function toggleSection(lnk){

 var td = lnk.parentNode;
 var table = getParent(td,'TABLE');
 var len = table.rows.length;
 var tr = getParent(td, 'tr');
 var rowIndex = tr.rowIndex;
 var rowHead=table.rows[rowIndex].cells[1].innerHTML;



lnk.innerHTML =(lnk.innerHTML == openImgHTML)?closedImgHTML:openImgHTML;

 vStyle =(tbl.rows[rowIndex+1].style.display=='none')?'':'none';

 for(var i = rowIndex+1; i < len;i++){
  if (table.rows[i].cells[1].innerHTML==rowHead){
    table.rows[i].style.display= vStyle;
    table.rows[i].cells[1].style.visibility="hidden";
  }
 }
}

function toggleRows(){
    tables =document.getElementsByTagName("table");
    for(i =0; i<tables.length;i++){
      if(tables[i].className.indexOf("expandable") != -1)
        tbl =tables[i];
    }
    if(typeof tbl=='undefined'){
     alert("Could not find a table of expandable class");
     return;
    }

//assume the first row is headings and the first column is empty
 var len = tbl.rows.length;
var link ='<a href="#" onclick="toggleSection(this);return false;" style="text-decoration: none">'+closedImgHTML+'</a>';

 var rowHead = tbl.rows[1].cells[1].innerHTML;

 for (j=1; j<len;j++){
  //check the value in each row of column 2
  var m = tbl.rows[j].cells[1].innerHTML;

if(m!=rowHead || j==1){
   rowHead=m;
   tbl.rows[j].cells[0].innerHTML = link;
//   tbl.rows[j].cells[0].style.textAlign="center";
   tbl.rows[j].style.background = "#FFFFFF";
  }
else
   tbl.rows[j].style.display = "none";
}

}
var oldEvt = window.onload;

var preload_image_1=new Image()
var preload_image_2=new Image()
preload_image_1.src=toggleimage[0]
preload_image_2.src=toggleimage[1]
var i_image=0

function testloading() {
    isloaded=true
}

function toggle() {
    if (isloaded) {
        document.togglepicture.src=toggleimage[i_image]
    }
    i_image++
    if (i_image>1) {i_image=0}
}

window.onload = function() { if (oldEvt) oldEvt(); toggleRows(); testloading();}


</SCRIPT>

<TABLE class=expandable width="400px" border="1"  cellspacing="0" frame="box" rules="all" >
  <THEAD>
  <TR>
    <TH  bgColor="#E6E4D4">&nbsp;</TH>
    <TH bgColor="#E6E4D4" align="left">Manager</TH>
    <TH bgColor="#E6E4D4" align="left">Sales Rep</TH>
    <TH bgColor="#E6E4D4" align="left">Amount </TH></TR>
</THEAD>
  <TBODY>
  <TR>
    <TD>&nbsp;</TD>
    <TD>Sarah Jones</TD>
    <TD><i>Georgia District Reps</i></TD>
    <TD>500000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>Sarah Jones</TD>
    <TD>Rex Smtih</TD>
    <TD>350000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>Sarah Jones</TD>
    <TD>Alex Anderson</TD>
    <TD>150000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>William Hobby</TD>
    <TD><i>Texas District Reps</i></TD>
    <TD>630000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>William Hobby</TD>
    <TD>Bill Smith</TD>
    <TD>410000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>William Hobby</TD>
    <TD>Simon Wilkes</TD>
    <TD>220000</TD></TR>
</TBODY></font></TABLE>
<br>
<br>
<a href="javascript:toggle()"></a>


</body>
</html>

没有图像的先前代码集(使用打开/关闭文本)在所有浏览器中都可以正常工作。

不确定IE或其他内容是否存在图像语法问题。它肯定是专注于图像,因为它没有图像就可以正常工作。

以下是在IE(及其他)中使用的版本(基于文本)的CodePen。

http://codepen.io/anon/pen/morpF

以下是“所有”浏览器版本“

的HTML
<html>
<head>
<style type="text/css">
table { empty-cells: show; }
cell {font-family:'Calibri';font-size:11.0pt;color: #000000;}
TD{font-family: Calibri; font-size: 10.5pt;}
TH{font-family: Calibri; font-size: 10.5pt; }
</style>

</head>
<body>
<SCRIPT type=text/javascript> 
var tbl;

function trim(str){
 return str.replace(/^\s*|\s*$/g,"");
}

function getParent(el, pTagName) {
 if (el == null) return null;
 else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase
  return el;
 else
  return getParent(el.parentNode, pTagName);
}

function toggleSection(lnk){

 var td = lnk.parentNode;
 var table = getParent(td,'TABLE');
 var len = table.rows.length;
 var tr = getParent(td, 'tr');
 var rowIndex = tr.rowIndex;
 var rowHead=table.rows[rowIndex].cells[1].innerHTML;

 lnk.innerHTML =(lnk.innerHTML == "+")?"--":"+";

 vStyle =(tbl.rows[rowIndex+1].style.display=='none')?'':'none';

 for(var i = rowIndex+1; i < len;i++){
  if (table.rows[i].cells[1].innerHTML==rowHead){
    table.rows[i].style.display= vStyle;
    table.rows[i].cells[1].style.visibility="hidden";
  }
 }
}

function toggleRows(){
    tables =document.getElementsByTagName("table");
    for(i =0; i<tables.length;i++){
      if(tables[i].className.indexOf("expandable") != -1)
        tbl =tables[i];
    }
    if(typeof tbl=='undefined'){
     alert("Could not find a table of expandable class");
     return;
    }

//assume the first row is headings and the first column is empty
 var len = tbl.rows.length;
 var link ='<a href="#" onclick="toggleSection(this);return false;" style="text-decoration: none">+</a>';

 var rowHead = tbl.rows[1].cells[1].innerHTML;

 for (j=1; j<len;j++){
  //check the value in each row of column 2
  var m = tbl.rows[j].cells[1].innerHTML;

if(m!=rowHead || j==1){
   rowHead=m;
   tbl.rows[j].cells[0].innerHTML = link;
//   tbl.rows[j].cells[0].style.textAlign="center";
   tbl.rows[j].style.background = "#FFFFFF";
  }
else
   tbl.rows[j].style.display = "none";
}

}
var oldEvt = window.onload;
window.onload = function() { if (oldEvt) oldEvt(); toggleRows();}
</SCRIPT>

<TABLE class=expandable width="400px" border="1"  cellspacing="0" frame="box" rules="all" >
  <THEAD>
  <TR>
    <TH width="10%"  bgColor="#E6E4D4">&nbsp;</TH>
    <TH bgColor="#E6E4D4" align="left">Manager</TH>
    <TH bgColor="#E6E4D4" align="left">Sales Rep</TH>
    <TH bgColor="#E6E4D4" align="left">Amount </TH></TR></THEAD>
  <TBODY>
  <TR class="cell">
    <TD>&nbsp;</TD>
    <TD>Sarah Jones</TD>
    <TD>&nbsp</TD>
    <TD>500000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>Sarah Jones</TD>
    <TD>Rex Smtih</TD>
    <TD>350000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>Sarah Jones</TD>
    <TD>Alex Anderson</TD>
    <TD>150000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>William Jones</TD>
    <TD>&nbsp;</TD>
    <TD>620000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>William Jones</TD>
    <TD>Bill Smith</TD>
    <TD>410000</TD></TR>
  <TR>
    <TD>&nbsp;</TD>
    <TD>William Jones</TD>
    <TD>Simon Wilkes</TD>
    <TD>220000</TD></TR>
</TBODY></font></TABLE></body>
</html>

谢谢!

1 个答案:

答案 0 :(得分:2)

不同的浏览器以不同方式序列化DOM。在IE10中运行代码给了我这个:

foo.innerHTML = "<img name=\"togglepicture\" src=\"http://www.iconlooker.com/user-content/uploads/wall/thumb/misc._icons_open_folder.png\" border=\"0\" height=\"50\">";

foo.innerHTML; // <img name="togglepicture" height="50" src="http://www.iconlooker.com/user-content/uploads/wall/thumb/misc._icons_open_folder.png" border="0">

IE重新排列了属性,因此您不再拥有相同的字符串。

不是在链接元素中获取HTML,而是获取图像本身并查看其src属性。