在 StackEdit 中可折叠

StackEdit 删除了某些 html 标签,使得实现可折叠按钮变得很麻烦。

你如何在 StackEdit 中实现可折叠部分?


隔江千里
浏览 138回答 1
1回答

沧海一幻觉

结果:将以下 CSS 插入到<head>StackEdit 模板的 中,并在标记末尾上方插入以下 javascript 代码body。<style>/* Style the button that is used to open and close the collapsible content */.collapsible {&nbsp; background-color: #eee;&nbsp; color: #444;&nbsp; cursor: pointer;&nbsp; padding: 18px;&nbsp; width: 100%;&nbsp; border: none;&nbsp; text-align: left;&nbsp; outline: none;&nbsp; font-size: 15px;}/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */.active, .collapsible:hover {&nbsp; background-color: #ccc;}/* Style the collapsible content. Note: hidden by default */.content {&nbsp; padding: 0 18px;&nbsp; display: none;&nbsp; overflow: hidden;&nbsp; background-color: #f1f1f1;}</style><script>var coll = document.getElementsByClassName("collapsible");var i;for (i = 0; i < coll.length; i++) {&nbsp; coll[i].addEventListener("click", function() {&nbsp; &nbsp; this.classList.toggle("active");&nbsp; &nbsp; var content = this.nextElementSibling;&nbsp; &nbsp; if (content.style.display === "block") {&nbsp; &nbsp; &nbsp; content.style.display = "none";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; content.style.display = "block";&nbsp; &nbsp; }&nbsp; });}</script>发布页面时使用您创建的模板,它应该类似于:&nbsp; &nbsp; <!DOCTYPE html>&nbsp; &nbsp; <html>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; &nbsp; <title>{{files.0.name}}</title>&nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="https://stackedit.io/style.css" />&nbsp; &nbsp; <style>&nbsp; &nbsp; /* Style the button that is used to open and close the collapsible content */&nbsp; &nbsp; .collapsible {&nbsp; &nbsp; &nbsp; background-color: #eee;&nbsp; &nbsp; &nbsp; color: #444;&nbsp; &nbsp; &nbsp; cursor: pointer;&nbsp; &nbsp; &nbsp; padding: 18px;&nbsp; &nbsp; &nbsp; width: 100%;&nbsp; &nbsp; &nbsp; border: none;&nbsp; &nbsp; &nbsp; text-align: left;&nbsp; &nbsp; &nbsp; outline: none;&nbsp; &nbsp; &nbsp; font-size: 15px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */&nbsp; &nbsp; .active, .collapsible:hover {&nbsp; &nbsp; &nbsp; background-color: #ccc;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /* Style the collapsible content. Note: hidden by default */&nbsp; &nbsp; .content {&nbsp; &nbsp; &nbsp; padding: 0 18px;&nbsp; &nbsp; &nbsp; display: none;&nbsp; &nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; &nbsp; background-color: #f1f1f1;&nbsp; &nbsp; }&nbsp; &nbsp; </style>&nbsp; &nbsp; </head>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; {{#if pdf}}&nbsp; &nbsp; <body class="stackedit stackedit--pdf">&nbsp; &nbsp; {{else}}&nbsp; &nbsp; <body class="stackedit">&nbsp; &nbsp; {{/if}}&nbsp; &nbsp; <a href="./" style="&nbsp; &nbsp; &nbsp; &nbsp; position:&nbsp; absolute;&nbsp; &nbsp; &nbsp; &nbsp; top:&nbsp; 10px;&nbsp; &nbsp; &nbsp; &nbsp; left:&nbsp; 10px;&nbsp; &nbsp; ">Back</a>&nbsp;&nbsp;&nbsp; &nbsp; <div class="stackedit__html">{{{files.0.content.html}}}</div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <script>&nbsp; &nbsp; var coll = document.getElementsByClassName("collapsible");&nbsp; &nbsp; var i;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for (i = 0; i < coll.length; i++) {&nbsp; &nbsp; &nbsp; coll[i].addEventListener("click", function() {&nbsp; &nbsp; &nbsp; &nbsp; this.classList.toggle("active");&nbsp; &nbsp; &nbsp; &nbsp; var content = this.nextElementSibling;&nbsp; &nbsp; &nbsp; &nbsp; if (content.style.display === "block") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content.style.display = "none";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content.style.display = "block";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; </script>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>现在可以创建可折叠部分:<div cursor="pointer" class="collapsible">Open Section 1</div><div class="content">&nbsp; <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript