编程语言
首页 > 编程语言> > Javascript显示/隐藏不在IE8中工作

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

作者:互联网

我有一些代码可用于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>

解决方法:

你尝试过className而不是getAttribute(“class”)吗?

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';
    }
  }
}

标签:javascript,show-hide
来源: https://codeday.me/bug/20190529/1180633.html