javascript - 单击菜单项时将类添加到 div

标签 javascript jquery html css

我不是最擅长 jQuery 的,但我尝试创建一个视频播放器,其中有一个子菜单,您可以单击以显示不同的内容选项。这是前端的图片(至少我可以做到!)-

image here

因此,当用户点击视频或音频时,它会显示包含内容的不同 div。

这是我的 html 标记:

<ul class="vdotb">
    <li><a href="#video"><i class="fa fa-video-camera"></i> Video </a></li>
    <li><a href="#audio"> <i class="fa fa-headphones"></i> Audio</a></li>
    <li class="subs"><a href="#subscribe"><i class="fa fa-microphone"></i> Subscribe to the Podcast</a></li>
</ul>
<div class="tabscontnt">Video Here</div>
<div class="tabscontnt">Audio Here</div>
<div class="tabscontnt">Subscribe info here</div>

我的 .tabs-active 类在我的 css 文件中显示为一个 block 。因此,当类“.tabs-active”应用于“tabscontnt”div 时,内容就会显示出来。

这是我失败的 jQuery...

$('ul.vdotb li a').click(

function(e) {
    e.preventDefault(); // prevent the default action
    e.stopPropagation; // stop the click from bubbling
    $(this).closest('ul').find('.tabs-active').removeClass('tabs-active');
    $(this).parent().addClass('tabs-active');
});

感谢您的帮助和宝贵时间!

最佳答案

您正在将类(class)应用于 <li>元素,而不是相应的 <div>元素。尝试为每个 <div> 添加一个 ID匹配 href<a>你点击,然后使用类似这样的东西来切换它们:

$('ul.vdotb li a').click(function(e){
  e.preventDefault(); // prevent the default action
  e.stopPropagation; // stop the click from bubbling

  //remove .tabs-active from any active tabs
  $('.tabscontnt.tabs-active').removeClass('tabs-active');
  //add .tabs-active to the corresponding div
  $($(this).attr('href')).addClass('tabs-active');
});

这是一个 fiddle :https://jsfiddle.net/upjyv8a0/

关于javascript - 单击菜单项时将类添加到 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37039629/

相关文章:

html - 图例在 Firefox 中没有正确居中与边距

javascript - innerHTML 分隔线不能在 IE6 中工作

javascript - GeoExt3 中的 'instanceof' 操作数 ol.layer.Base 无效

javascript - 为什么这个 Javascript 没有添加到 HTML 中

jQuery .load() 函数正在执行我的 <HttpPost()> 操作而不是我的 <HttpGet()>

javascript - 页面加载后滑动

javascript/jquery 数组交互在 .on ("click"之后不起作用)

javascript - 是否可以在 Javascript 中覆盖 window.location.hostname?

jquery - 在打开参数之前将 HTML 放入 jQuery 模式对话框中

javascript - 多个图像预览未显示