Javascript显示/隐藏不在IE8中工作

时间:2012-11-26 14:54:49

标签: javascript show-hide

我有一些代码可以在FF,Chrome,Safari甚至IE9中运行。当然,它在IE8中不起作用。这是使用Javascript在两个div上显示/隐藏。我对JS并不十分精通,所以任何帮助都会受到赞赏。

Javascript功能:

function showonlyone(thechosenone) {
  var subscriberinfo = document.getElementsByTagName("div");
  for(var x=0; x<subscriberinfo.length; x++) {
    name = subscriberinfo[x].getAttribute("class");
    if (name == 'subscriberinfo') {
      if (subscriberinfo[x].id == thechosenone) {
        subscriberinfo[x].style.display = 'block';
      } else {
        subscriberinfo[x].style.display = 'none';
      }
    }
  }
}

HTML code:

<ul class="options">
  <div class="subscriber-options">
    <a href="javascript:showonlyone('subscriberinfo1');" >Account</a>
  </div>
  <div class="subscriber-options">
    <a href="javascript:showonlyone('subscriberinfo2');" >Subscriber Options</a>
  </div>
</ul>
<!-- options -->
<div class="subscriberinfo" id="subscriberinfo1">Div #1</div>
<!-- subscriberinfo1 -->
<div class="subscriberinfo" id="subscriberinfo2" style="display: none;">Div #2</div>

1 个答案:

答案 0 :(得分:6)

而不是getAttribute("class")您尝试className了吗?

function showonlyone(thechosenone) {
  var subscriberinfo = document.getElementsByTagName("div");

  for(var x=0; x<subscriberinfo.length; x++) {
    name = subscriberinfo[x].className;  // <-- Here is the change
    if (name == 'subscriberinfo') {
      subscriberinfo[x].style.display = 
          (subscriberinfo[x].id == thechosenone) ? 'block' : 'none';
    }
  }
}