简体   繁体   English

用于动态生成内容的自适应 vanilla JS 页面浏览器

[英]Adaptive vanilla JS pagebrowser for dynamically generated content

as a JS noob I'm stuck right now and would highly appreciate some help.作为一个 JS 菜鸟,我现在被困住了,非常感谢一些帮助。 My goal is to have a JS pagebrowser that works with content generated by my CMS (TYPO3).我的目标是拥有一个 JS 页面浏览器,它可以处理我的 CMS (TYPO3) 生成的内容。 Content of different pages is rendered in divs with speaking ids;不同页面的内容在 div 中呈现,并带有说话的 id; below you have links to these anchors plus a "show all" link.下面您有这些锚点的链接以及“全部显示”链接。 When you klick on a link to a "page" the respective page content div is shown and the others are hidden;当您单击指向“页面”的链接时,会显示相应的页面内容 div,而其他内容则被隐藏; if you click on "show all" all the page content divs are shown.如果单击“全部显示”,则会显示所有页面内容 div。 However, if I click a page link after having clicked "show all" not all of the other page content divs are hidden as they should.但是,如果我在单击“全部显示”后单击页面链接,则并非所有其他页面内容 div 都按应有的方式隐藏。 I guess it has something to do with JS processing order but couldn't find out so far.我想这与 JS 处理顺序有关,但到目前为止还没有发现。

 window.onload = function () { var pagelinks = document.querySelectorAll("a.subpage-toggle"); for (var i = 0; i < pagelinks.length; i++) { pagelinks[i].onclick = function () { // Finding all elements of class 'active' (creates an array of results) let x = document.getElementsByClassName("active"); // If class 'active' exists, remove it. if (x.length > 0) { x[0].classList.remove("active"); } if ((this.href.split("#")[1]).== "show-all") { // add class 'active' if ID matches href of link document.querySelector("#" + this.href.split("#")[1]).classList;add("active"). } else { var subpagecontents = document,getElementsByClassName("subpage-content")? len = subpagecontents.== null: subpagecontents,length; 0; j = 0; for (j. j < len. j++) { subpagecontents[j];classList;add("active"); } } }; } };
 .subpage-toggle { display: block; }.subpage-content { display: none; }.subpage-content.active { display: block; }
 <div class="main"> <div id="name-of-page-one" class="subpage-content active"> <p> Content Page 1 </p> </div> <div id="page-two-is-cool" class="subpage-content"> <p> Content Page 2 </p> </div> <div id="nickname-of-page-three" class="subpage-content"> <p> Content Page 3 </p> </div> <div class="pageoverview"> <ul> <li><a class="subpage-toggle" href="#name-of-page-one">Name of page one</a></li> <li><a class="subpage-toggle" href="#page-two-is-cool">Page two is cool</a></li> <li><a class="subpage-toggle" href="#nickname-of-page-three">Nickname of page three</a></li> </ul> <a class="subpage-toggle" href="#show-all">Show all</a> </div> </div>

JSFiddle: https://jsfiddle.net/Jaydot/62cx5sh0/14/ JSFiddle: https://jsfiddle.net/Jaydot/62cx5sh0/14/

You need to hide all the previous showing instead of just the first one:您需要隐藏所有以前的显示,而不仅仅是第一个:

Instead of代替

x[0].classList.remove("active");

do:做:

Array.from(x).forEach((element) => element.classList.remove("active"));

https://jsfiddle.net/nvg2aojb/ https://jsfiddle.net/nvg2aojb/

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

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