简体   繁体   English

动态选项卡不显示内容 Javascript

[英]Dynamic tab doesn't show content Javascript

Currently I have 6 tabs that are dynamically populated from the Post Categories in Wordpress.目前,我有 6 个选项卡是从 Wordpress 中的帖子类别动态填充的。 https://nationalvip.pixl.work/webinars/ https://nationalvip.pixl.work/webinars/

When I click each of them they should be displaying the posts belonging to that category selected.当我单击它们中的每一个时,它们应该显示属于所选类别的帖子。

Problem is If I click any of them the one selected doesn't show the anything.问题是如果我单击其中任何一个,则所选的一个不会显示任何内容。

here is my javascript code (which i took it from here: W3 Schools :这是我的 javascript 代码(我从这里获取的: W3 Schools

function openCity(evt, cityName) {

    var i, tabcontent, tablinks;

    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
 }

    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }

    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}

I've done some tests, and found that the "active" class is not added to the selected tab.我做了一些测试,发现“活动” class 没有添加到选定的选项卡中。 Although this same code works on codepen here: CODEPEN虽然同样的代码适用于这里的 codepen: CODEPEN

You can see my current demo here: https://nationalvip.pixl.work/webinars/你可以在这里看到我当前的演示: https://nationalvip.pixl.work/webinars/

I really appreciate your help.我真的很感谢你的帮助。 Thanks谢谢

Just in case it helps someone with same issue:以防万一它可以帮助有同样问题的人:

the problem was this line: document.getElementById(cityName).style.display = "block";问题是这一行: document.getElementById(cityName).style.display = "block";

I updated that line with this one: cityName.style.display = "block";我用这个更新了这一行: cityName.style.display = "block";

we need to use the value received on the cityName which contain the ID of the block to be displayed.我们需要使用 cityName 收到的值,其中包含要显示的块的 ID。

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

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