繁体   English   中英

页面加载时可折叠

[英]Collapsible is open when page loads

我根本不擅长 Javascript,我只知道非常基础的知识。 我安装了 Disqus 作为测试网页的插件,该网页应该放在单独的帖子上。 因此,我制作了一个带有“显示评论”按钮的可折叠组件,以便在页面加载时隐藏插件,但是一旦单击“显示评论”,您就可以看到帖子的评论。 我使用了 w3schools 的可折叠示例,它默认关闭可折叠。 出于某种原因,它与我的不同,每当我加载页面时,可折叠页面都是打开的。 否则它会正常工作。 我已经尝试了一些与我类似的其他问题的解决方案,但没有奏效,而且,由于我不擅长 Javascript,所以我可能做错了。 我还想知道是否因为可折叠的内容是一个插件,这就是它保持打开状态的原因,但同样,我不确定它是如何工作的。 这是网页:

https://appcom.webaccident.com.au/IT011/studentmasters/Site/explore.html

如果您也想查看样式表,请访问: https : //appcom.webadd.com.au/IT011/studentmasters/Site/createstylesheet.css

对不起,如果这是一个愚蠢的问题,任何帮助表示赞赏。

谢谢 :)

编辑:我发现课程前面没有句点,所以我修复了这个问题,并且它的显示设置为无,但它仍然不起作用,所以我不知道该怎么做。

我看看能不能把代码复制粘贴进去。

<button type="button" class="collapsible">Show Comments</button>
            <div class="comments" id="disqus_thread"></div>
<script>

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
/*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://studentmasters.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

        <script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
</script>

用于评论的 css 如下所示:

.comments {
  display: none;
  overflow: hidden;
}

添加display: none; <div class="comments" id="disqus_thread" style="display: none;"></div>

我无法使用您提供的代码复制您的问题,显示:无; 在你的评论课上对我来说很好。

对于您所追求的功能,我不建议放置display:none; 在 CSS 的注释类中。

当您导入其他人的 HTML 时,您的 CSS 可能会被覆盖。

您使用的 JS 代码也会影响内联样式,而不是类样式。

 // Your comments code (function() { var d = document, s = d.createElement('script'); s.src = 'https://studentmasters.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })(); // Your show/hide code var coll = document.getElementsByClassName("collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; } }); }
 .comments { overflow: hidden; }
 <button type="button" class="collapsible">Show Comments</button> <div class="comments" id="disqus_thread" style="display:none;"></div>

暂无
暂无

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

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