9

我在stackoverflow中找到了这个脚本。

 function showhide(id){
        if (document.getElementById) {
          var divid = document.getElementById(id);
          var divs = document.getElementsByClassName("hideable");
          for(var div in divs) {
             div.style.display = "none";
          }
          divid.style.display = "block";
        } 
        return false;
       }

<a href="#" onclick="showhide('features');" >features</a>
<a href="#" onclick="showhide('design');"  >design</a>
<a href="#" onclick="showhide('create');" >create</a>

<div class="hideable" id="features">Div 1</div>
<div class="hideable" id="design">Div 2</div>
<div class="hideable" id="create">Div 3</div>

但它说, div.style 未定义。为什么?:)

4

6 回答 6

11

你不应该为此使用 for-in 循环。

document.getElementsByClassName('someClass')

返回一个NodeList,它不继承自Array.prototype,但在某些方面是相似的。就像名字所说的,它是一个节点列表。这意味着它有一个length属性,并且只能使用以下方式访问:

var myElements = document.getElementsByClassName('yourClass');
for (var i = 0, ii = myElements.length; i < ii; i++) {
    console.dir(myElements[i].style);
};

这里是你应该如何真正隐藏一个元素。

/**
 * Shows or hides an element from the page. Hiding the element is done by
 * setting the display property to "none", removing the element from the
 * rendering hierarchy so it takes up no space. To show the element, the default
 * inherited display property is restored (defined either in stylesheets or by
 * the browser's default style rules.)
 *
 * Caveat 1: if the inherited display property for the element is set to "none"
 * by the stylesheets, that is the property that will be restored by a call to
 * showElement(), effectively toggling the display between "none" and "none".
 *
 * Caveat 2: if the element display style is set inline (by setting either
 * element.style.display or a style attribute in the HTML), a call to
 * showElement will clear that setting and defer to the inherited style in the
 * stylesheet.
 * @param {Element} el Element to show or hide.
 * @param {*} display True to render the element in its default style,
 * false to disable rendering the element.
 */
var showElement = function(el, display) {
  el.style.display = display ? '' : 'none';
};

var myElement = document.getElementById('someID');
showElement(myElement, false);// it should now be hidden.
于 2013-01-15T10:01:42.883 回答
4
for(var i = 0; i < divs.length; i++) {
         divs[i].style.display = "none";
      }

编辑:for in 循环用于循环对象属性

于 2013-01-15T09:58:36.807 回答
2

代替

for(var div in divs) {

for(var i=0; i<divs.length;i++) {
   var div = divs[i];

divsgetElementsByClassName的结果,实际上不是一个数组,而是一个 NodeList,一个类似数组的对象,您正在迭代它的属性,而不是它的元素。

于 2013-01-15T09:59:29.787 回答
0

在许多 webapps 不再支持 IE 的日子里,使用for ... of循环正是您当时所寻找的:

function showhide(id) {
  if (document.getElementById) {
    let divid = document.getElementById(id);
    let divs = document.getElementsByClassName("hideable");
    for (let div of divs) {
        div.style.display = "none";
    }
    divid.style.display = "block";
  }
  return false;
}
于 2020-05-07T20:21:39.617 回答
0

在 javascript for(var div in divs) 中的行为不像 foreach。在这种情况下,'div' 是索引。所以元素应该是:divs[div]

于 2019-10-19T19:27:21.697 回答
-2

确保for-in循环中的所有元素都是 DOM 元素。for-in用 a 过滤循环是一个好习惯hasOwnProperty()

      for(var div in divs) {
           if (divs.hasOwnProperty(div)) {
               if (div && div.style) {
                   div.style.display = "none";
               }
           }
      }
于 2013-01-15T09:58:00.997 回答