简体   繁体   English

单击菜单,将div内容替换为隐藏div

[英]Replace div Content with hidden div on click from menu

I'm would like to design a video gallery with multiple menu tabs. 我想设计一个包含多个菜单选项卡的视频库。 There are 24 videos and 6 tabs(4 under each). 共有24个视频和6个标签(每个标签下4个)。 I am new to JavaScript/jQuery and I'm not sure where to start. 我是JavaScript / jQuery的新手,我不知道从哪里开始。 Do I have to create a function onClick? 我是否必须在onClick上创建一个函数?

The way I see it is this: There is a main display div where the video selected from the menu will appear. 我看到它的方式是:有一个主显示div,从菜单中选择的视频将出现。 By default, when the page loads, the first video appears and then will be replaced by whatever video is chosen from the link that is clicked from the menu. 默认情况下,当页面加载时,将显示第一个视频,然后将替换为从菜单中单击的链接中选择的任何视频。 In this example, I want the div that is selected from the menu to replace the div "active-demo" from "inactive" 在这个例子中,我希望从菜单中选择的div将div“active-demo”替换为“inactive”

I want to know how I can hide the other 23 videos while one is selected. 我想知道如何在选择一个视频时隐藏其他23个视频。

Should I use display: block/none or visibility: hidden/invisible or is there something else I can use(JS or jQuery). 我应该使用display:block / none或visibility:hidden / invisible还是我可以使用的其他东西(JS或jQuery)。

Here is the code I have so far: 这是我到目前为止的代码:

HTML HTML

<div>
  <ul>
    <li>
      <a href='#demo'>Menu</a>
      <ul>
        <li>
          <a href='#demo1'>Demo 1</a>
        </li>
        <li>
          <a href='#demo2'>Demo 2</a>
        </li>
        <li>
          <a href='#demo3'>Demo 3</a>
        </li>
        <li>
          <a href='#demo4'>Demo 4</a>
        </li>
      </ul>
    </li>
</div>

<div class="active-demo">
  <div id="demo1">
    <iframe src='https://player.vimeo.com/video/9153533' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
  </div>
</div>

<div id="demo2" class="inactive">
  <iframe src='https://player.vimeo.com/video/2112265' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

<div id="demo3" class="inactive">
  <iframe src='https://player.vimeo.com/video/2112264' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

<div id="demo4" class="inactive">
  <iframe src='https://player.vimeo.com/video/9153534' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

CSS CSS

.active-demo {
 display:block;
}

.inactive {
  display:none;
}

Demo : 演示:

https://jsfiddle.net/ajaymz/L1xjtwvr/ https://jsfiddle.net/ajaymz/L1xjtwvr/

I would first add a class called "tab-link" to each of the tab selector links to make them easy to select with the jQuery. 我首先要为每个选项卡选择器链接添加一个名为“tab-link”的类,以便使用jQuery轻松选择它们。 Then, your jQuery would look something like this: 然后,你的jQuery看起来像这样:

$(function() {
     $('.tab-link').on('click', function() {
          // Switch the class on the previously active div to make it hidden
          $('.active-demo').removeClass('active-demo').addClass('inactive');
          // Switch the class on the new active div to show it
          var selectorForActiveDemo = $(this).attr('href');
          $(selectorForActiveDemo).removeClass('inactive').addClass('active-demo');
     });
});

EDIT: 编辑:

I realized later than your first demo has an extra div around it that keeps the link for the first one from working. 我后来意识到,你的第一个演示有一个额外的div,它保持第一个的链接工作。 Removing that div and adding the active-div class to the one it enclosed should allow it to work. 删除该div并将active-div类添加到它所包含的类应允许它工作。

I put the working code on a JSFiddle: https://jsfiddle.net/9h6a4ckp/ 我将工作代码放在JSFiddle上: https ://jsfiddle.net/9h6a4ckp/

This works 这有效

HTML HTML

 $(function() { $('a').on('click', function() { var val = $(this).attr('href'); $('.video').find('div').each(function() { if(!($(this).hasClass('inactive'))) { $(this).addClass('inactive'); } }) $('.video').find('div').each(function() { if(('#' + $(this).attr('id')) === val) { alert($(this).attr('id')) $(this).removeClass('inactive'); } }) }) }) 
 .active-demo { display:block; } .inactive { display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <ul> <li> <a href='#demo'>Menu</a> <ul> <li> <a href='#demo1'>Demo 1</a> </li> <li> <a href='#demo2'>Demo 2</a> </li> <li> <a href='#demo3'>Demo 3</a> </li> <li> <a href='#demo4'>Demo 4</a> </li> </ul> </li> </ul> </div> <div class="video"> <div class="active-demo"> <div id="demo1"> <iframe src='https://player.vimeo.com/video/9153533' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe> </div> </div> <div id="demo2" class="inactive"> <iframe src='https://player.vimeo.com/video/2112265' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe> </div> <div id="demo3" class="inactive"> <iframe src='https://player.vimeo.com/video/2112264' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe> </div> <div id="demo4" class="inactive"> <iframe src='https://player.vimeo.com/video/9153534' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe> </div> </div> 

DEMO JSfiddle DEMO JSfiddle

What I am doing is finding which link is clicked and then removing class inactive from the corresponding div and before that adding class inactive to the div which did not have it. 我正在做的是找到点击了哪个链接,然后从相应的div删除了类inactive ,然后在没有它的div中将类添加到inactive

<div>
  <ul>
    <li>
      <a href='#demo'>Menu</a>
      <ul>
        <li>
          <a href='#demo1'>Demo 1</a>
        </li>
        <li>
          <a href='#demo2'>Demo 2</a>
        </li>
        <li>
          <a href='#demo3'>Demo 3</a>
        </li>
        <li>
          <a href='#demo4'>Demo 4</a>
        </li>
      </ul>
    </li>
   </ul> <!-- add missing ul close -->
</div>

<div id="demo1" class="active-demo"> <!-- put id in the div -->
    <iframe src='https://player.vimeo.com/video/9153533' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
<div id="demo2" class="inactive">
  <iframe src='https://player.vimeo.com/video/2112265' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

<div id="demo3" class="inactive">
  <iframe src='https://player.vimeo.com/video/2112264' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

<div id="demo4" class="inactive">
  <iframe src='https://player.vimeo.com/video/9153534' width="500" height="315" frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

This should work: Don't forget to add jquery.js. 这应该工作:不要忘记添加jquery.js。 You will need to for jquery to work 你需要让jquery工作

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
// prevent executing script when #demo being clicked
$('ul li ul li a').click(function(e){
// prevent default behaviour of a
e.preventDefault();
// reset any .active-demo to .inactive class
$('.active-demo').addClass('inactive').removeClass('active-demo');
// get the value of the href attribute that clicked
var str = $(this).attr('href');
// remove the hash from that value
var target = str.substring(1, str.length);
// change the target id's class from .inactive to .active-demo
$('#'+target).addClass('active-demo').removeClass('inactive');
});
</script>

DEMO: DEMO:

https://jsfiddle.net/b3ks0ybr/ https://jsfiddle.net/b3ks0ybr/

Instead of hide and unhide: 而不是隐藏和取消隐藏:

THIS IS WHAT YOU CAN DO: 这是你可以做的:

<div>
  <ul>
    <li>
      <a href='#demo'>Menu</a>
      <ul>
        <li>
          <a href='https://player.vimeo.com/video/9153533'>Demo 1</a>
        </li>
        <li>
          <a href='https://player.vimeo.com/video/2112265'>Demo 2</a>
        </li>
        <li>
          <a href='https://player.vimeo.com/video/2112264'>Demo 3</a>
        </li>
        <li>
          <a href='https://player.vimeo.com/video/9153534'>Demo 4</a>
        </li>
      </ul>
    </li>
  </ul>
</div>

<div id="active-demo"><iframe src="https://player.vimeo.com/video/9153533" width="500" height="315" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
// prevent executing script when clicking #demo
$('ul li ul li a').click(function(e){
  // prevent default behaviour of click a tag
  e.preventDefault();
  // get the link from href that clicked
  var src = $(this).attr('href');
  // write the iframe code together with the link inside div id active-demo
  $('#active-demo').html('<iframe src="' + src + '" width="500" height="315" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>');
});
</script>

HERE IS THE EXAMPLE: 这是例子:

https://jsfiddle.net/ef8xrtkg/ https://jsfiddle.net/ef8xrtkg/

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

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