简体   繁体   English

如何使用JavaScript隐藏和显示多个Div

[英]How To Hide And Display Multiple Divs using JavaScript

How would I go about hiding and showing multiple divs using JavaScript? 如何使用JavaScript隐藏和显示多个div? I don't want to use JQuery. 我不想使用JQuery。 I can make it work for hiding and showing one div but not multiple divs. 我可以使其隐藏和显示一个div,但不能显示多个div。 The problem originates because I'm using PHP to display multiple records. 产生此问题的原因是我正在使用PHP显示多个记录。 These records are included in divs which have the same ID. 这些记录包含在具有相同ID的div中。

 document.getElementById( 'history-slider' ).addEventListener( 'click', function() { document.getElementById('edit-slider').style.display = 'block'; document.getElementById('history-slider').style.display = 'none'; }, false ); document.getElementById( 'edit-slider' ).addEventListener( 'click', function() { document.getElementById('history-slider').style.display = 'block'; document..getElementById('edit-slider').style.display = 'none'; }, false ); 
 .edit-slider { display: none; } 
 <div class="panel-body panel-strip" id="history-slider"> <h3>Title</h3> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> <p> <img src="img/time_icon.png" class="time-icon"> <span class="hour-text">4.00 hrs</span> </p> </div> <hr class="calendar-divider"> <div class="panel-body panel-strip edit-slider"> <div class="row pull-right"> <a href="add.php"> <div class="col-xs-4 delete-panel"> <img src="img/delete_icon.png" class="edit-images center-block"><span class="text-center edit-texts">Delete</span> </div> </a> <a href="http://google.com/"> <div class="col-xs-4 edit-panel"> <img src="img/edit_icon.png" class="edit-images center-block"><span class="text-center edit-texts edit-text">Edit</span> </div> </a> <a href="http://google.com/"> <div class="col-xs-4 record-panel"> <img src="img/record_icon.png" class="edit-images center-block"><span class="text-center edit-texts">Record</span> </div> </a> </div> </div> 

HTML; HTML;

                    <div class="panel-body panel-strip" id="history-slider">
                        <h3>Title</h3>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
                        <p>
                          <img src="img/time_icon.png" class="time-icon"> <span class="hour-text">4.00 hrs</span>
                        </p>
                    </div>
            <hr class="calendar-divider">
            <div class="panel-body panel-strip edit-slider">
                <div class="row pull-right">
                    <a href="add.php">
                        <div class="col-xs-4 delete-panel">
                            <img src="img/delete_icon.png" class="edit-images center-block"><span class="text-center edit-texts">Delete</span>
                        </div>
                        </a>
                        <a href="http://google.com/">
                        <div class="col-xs-4 edit-panel">
                            <img src="img/edit_icon.png" class="edit-images center-block"><span class="text-center edit-texts edit-text">Edit</span>
                        </div>
                        </a>
                        <a href="http://google.com/">
                        <div class="col-xs-4 record-panel">
                            <img src="img/record_icon.png" class="edit-images center-block"><span class="text-center edit-texts">Record</span>
                        </div>
                        </a>
                    </div>
                </div>

JavaScript; JavaScript;

document.getElementById( 'history-slider' ).addEventListener( 'click', function() {

  document.getElementById('edit-slider').style.display = 'block';
  document.getElementById('history-slider').style.display = 'none';

}, false );

document.getElementById( 'edit-slider' ).addEventListener( 'click', function() {

  document.getElementById('history-slider').style.display = 'block';
  document..getElementById('edit-slider').style.display = 'none';

}, false );

I have also set in the CSS to hide the "edit-slider" div on page load. 我还设置了CSS,以在页面加载时隐藏“ edit-slider” div。

.edit-slider {
    display: none;
}

The HTML is echoed out in a loop for every record in the database. 对于数据库中的每个记录,HTML都会在循环中回显。 Information is also added in replace of the placeholder text. 还添加了信息,以代替占位符文本。

How should I best go about making it so that if a div if clicked it is hidden and the corresponding div is shown in it's place? 我怎样才能最好地做到这一点,以便在单击div时将其隐藏并在其位置显示相应的div?

I was thinking about doing something about individually giving the divs separate ID's in PHP and than passing those ID's to JavaScript and creating some sort of a loop? 我正在考虑做一些事情,以便在PHP中分别给div单独的ID,而不是将这些ID传递给JavaScript并创建某种循环? My knowledge of JavaScript isn't massive so I don't really know how easy or difficult this method would be. 我对JavaScript的知识不是很丰富,所以我真的不知道这种方法有多容易。 Or is there a much easier method? 还是有更简单的方法?

This is my first stack overflow post,so sorry if I'm doing anything wrong or missed something. 这是我的第一个堆栈溢出帖子,对不起,如果我做错了什么或错过了什么。

If you use classes instead of IDs you can use document.QuerySelectorAll() to get all the divs with that class and then show or hide as necessary. 如果使用类而不是ID,则可以使用document.QuerySelectorAll()获取该类的所有div,然后根据需要显示或隐藏。

Something like below would hide all divs with an edit-slider class and reveal (assuming they were already hidden) all divs with a history-slider class. 如下所示将隐藏所有具有edit-slider类的div,并显示(假设它们已被隐藏)具有历史滑块类的所有div。

    (function() {
        var editSliders = document.querySelectorAll('div.edit-slider');
        for(var i=0;i<editSliders.length;i++){
            editSliders[i].style.display = 'none';
        }
        var historySliders = document.querySelectorAll('div.history-slider');
        for(var i=0;i<historySliders.length;i++){
            historySliders[i].style.display = 'block';
        }
    })();

First, consider using class instead of id to set multiple elements with the same attribute and value. 首先,考虑使用class而不是id来设置具有相同属性和值的多个元素。 Then, use the following script: 然后,使用以下脚本:

<script type="text/javascript">
  document.QuerySelectorAll('div.history-slider').setAttribute("onclick", "myFunction()");
  function myFunction() {
    this.hide;
  }
</script>

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

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