4

我有这段代码可以在 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 代码:

<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>
4

1 回答 1

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';
    }
  }
}
于 2012-11-26T15:05:16.640 回答