简体   繁体   English

显示/隐藏具有相同类名的Div - Javascript

[英]Showing/Hiding Divs with the same class name - Javascript

I am trying to replicate something similar to what the Google Javascript from the Ground up Accomplishes 我试图复制一些类似于来自Ground up的Google Javascript的内容

http://code.google.com/edu/submissions/html-css-javascript/#javascript http://code.google.com/edu/submissions/html-css-javascript/#javascript

Basically have multiple div classes with the same name and show/hide those based on adding removing classes to the original class name. 基本上有多个具有相同名称的div类,并根据将删除类添加到原始类名来显示/隐藏它们。

Heres my markup 继承我的标记

 <div class="vidItem" id="vidItem">
 <div class="vidTitle">
<h2></h2>
 </div>
 <div class="vidContain" id="vidContain">
 <iframe class="testtt" width="560" height="315" src="-----" frameborder="0" allowfullscreen>         </iframe>
</div>
   </div>

  <div class="vidItem" id="vidItem">
   <div class="vidTitle">
   <h2></h2>
   </div>
<div class="vidContain" id="vidContain">
    <iframe width="560" height="315" src="----" frameborder="0" allowfullscreen></iframe>
</div>
   </div>

Heres my javascript 继承人我的javascript

var toggleExpando = function() {
 var expando = this.parentNode;

  if (hasClass(expando, 'hide')) {

      removeClass(expando, 'hide');
      addClass(expando, 'show');

  } else {

      removeClass(expando, 'show');
      addClass(expando, 'hide');
}
};


var expandos = getElementsByClass('vidContain');
for (var i=0; i < expandos.length; i++) {
     addClass(expandos[i], 'hide');
     var expandoTitle = document.getElementById('vidItem');
     addEventSimple(expandoTitle, 'click', toggleExpando);
}

   }

Onload both of the divs seem to set their classes to hide just fine but when I click on the top one everything disappears but when I click on the bottom one nothing happens. Onload这两个div似乎设置他们的类隐藏得很好,但是当我点击顶部时,一切都消失了但是当我点击底部时没有任何反应。 I am assuming there is a problem with my for loop and also where it says (expando = this.parentNode). 我假设我的for循环和它所说的地方有问题(expando = this.parentNode)。 I have no idea where to go from here. 我不知道从哪里开始。

Any help or ideas would be appreciated. 任何帮助或想法将不胜感激。

Javascript assumes that there is only one element with specific id (and this is what standard say). Javascript假设只有一个元素具有特定的id(这就是标准所说的)。 So when you say.. 所以当你说..

var expandoTitle = document.getElementById('vidItem');

here the variable contains only first item with id vidItem and attaches event to that element only. 这里变量只包含id为vidItem第一个项目,并仅将事件附加到该元素。

This can be corrected by using class names instead of id. 这可以通过使用类名而不是id来更正。

jQuery might be a good option for this. jQuery可能是一个很好的选择。 ID needs to be unique but classes don't, so you can query for all element with a class name in the same way you would with id's in straight javascript by using jQuery. ID需要是唯一的,但类不需要,因此您可以使用jQuery以与使用jQuery中的id相同的方式查询具有类名的所有元素。 Not sure on what you want the initial state to be so i'm hiding all on inital load. 不确定你想要的初始状态是什么,所以我隐藏所有的初始负荷。 Something along the lines of below is what i mean. 我的意思是下面的东西。 (code is untested. place it in the html head) (代码未经测试。将其放在html头中)

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('.vidItem').addClass('hide');
    $('.vidItem').click(function(){
        $('.vidItem').addClass('hide');
        $(this).removeClass('hide'); 
        $(this).addClass('show');
    });
});
</script>

If you want to make it look nice with transition effects you can use some of the build in jQuery ones such as: 如果你想使用过渡效果使它看起来不错,你可以使用jQuery中的一些构建,例如:

$('.vidItem').fadeOut();
$(this).fadeIn();

or 要么

$('.vidItem').slideUp();
$(this).slideDown();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM