我很高兴能尝试this代码折叠方法,但我在实现它时遇到了麻烦。我以前从未使用过javascript,也不知道如何添加div包装器。它应该是代码块(灰色背景)的一部分,还是在代码块前后的常规文本?如果是前者,每当我试图将` div {r}向下移动1行以便为第一次调用腾出空间时,块就会消失。如果是后者,我在编织时就得不到预期的效果。
下面是一个可重复使用的示例:
---
title: "Toy markdown doc for js"
author: "Carrie Perkins"
date: "1/10/2021"
output: html_document
---
```{js}
$(文档)函数(.ready(){
$chunks = $('.fold');
$chunks.each(函数() {
// add button to source code chunks
if ( $(this).hasClass('s') ) {
$('pre.r', this).prepend("<div class=\"showopt\">Show Source</div><br style=\"line-height:22px;\"/>");
$('pre.r', this).children('code').attr('class', 'folded');
}
// add button to output chunks
if ( $(this).hasClass('o') ) {
$('pre:not(.r)', this).has('code').prepend("<div class=\"showopt\">Show Output</div><br style=\"line-height:22px;\"/>");
$('pre:not(.r)', this).children('code:not(r)').addClass('folded');
// add button to plots
$(this).find('img').wrap('<pre class=\"plot\"></pre>');
$('pre.plot', this).prepend("<div class=\"showopt\">Show Plot</div><br style=\"line-height:22px;\"/>");
$('pre.plot', this).children('img').addClass('folded');
}
});
//加载文档时隐藏所有分块
$('.folded').css('display','none')
//切换可见性的函数
$(‘.showopt’)函数(.click(){
var label = $(this).html();
if (label.indexOf("Show") >= 0) {
$(this).html(label.replace("Show", "Hide"));
} else {
$(this).html(label.replace("Hide", "Show"));
}
$(this).siblings('code, img').slideToggle('fast', 'swing');
});
});
```{js}
.showopt {
背景色:#004c93;
颜色:#FFFFFF;
宽度: 100px;
高度: 20px;
文本对齐:居中;
竖直对齐:居中!重要;
float:右;
font-family: sans-serif;
边界半径: 8px;
}
.showopt:hover {
background-color: #dfe4f2;
color: #004c93;
}
pre.plot {
背景色:白色!重要;
}
<div class="fold s o">
```{r cars}
摘要(汽车)
</div>
这是我在编织的时候得到的:
发布于 2021-01-11 00:11:17
不太确定为什么`{js}不能工作,但如果你用<script>
和<style>
标签替换它,它似乎可以正常工作。
---
title: "Toy markdown doc for js"
author: "Carrie Perkins"
date: "1/10/2021"
output: html_document
---
<script>
$(document).ready(function() {
$chunks = $('.fold');
$chunks.each(function () {
// add button to source code chunks
if ( $(this).hasClass('s') ) {
$('pre.r', this).prepend("<div class=\"showopt\">Show Source</div><br style=\"line-height:22px;\"/>");
$('pre.r', this).children('code').attr('class', 'folded');
}
// add button to output chunks
if ( $(this).hasClass('o') ) {
$('pre:not(.r)', this).has('code').prepend("<div class=\"showopt\">Show Output</div><br style=\"line-height:22px;\"/>");
$('pre:not(.r)', this).children('code:not(r)').addClass('folded');
// add button to plots
$(this).find('img').wrap('<pre class=\"plot\"></pre>');
$('pre.plot', this).prepend("<div class=\"showopt\">Show Plot</div><br style=\"line-height:22px;\"/>");
$('pre.plot', this).children('img').addClass('folded');
}
});
// hide all chunks when document is loaded
$('.folded').css('display', 'none')
// function to toggle the visibility
$('.showopt').click(function() {
var label = $(this).html();
if (label.indexOf("Show") >= 0) {
$(this).html(label.replace("Show", "Hide"));
} else {
$(this).html(label.replace("Hide", "Show"));
}
$(this).siblings('code, img').slideToggle('fast', 'swing');
});
});
</script>
<style>
.showopt {
background-color: #004c93;
color: #FFFFFF;
width: 100px;
height: 20px;
text-align: center;
vertical-align: middle !important;
float: right;
font-family: sans-serif;
border-radius: 8px;
}
.showopt:hover {
background-color: #dfe4f2;
color: #004c93;
}
pre.plot {
background-color: white !important;
}
</style>
<div class="fold s o">
```{r cars}
摘要(汽车)
</div>
话虽如此,我还是建议您使用内置的代码折叠选项,看看它是否可以做您需要的事情,而不是依赖于自定义的JS代码:https://bookdown.org/yihui/rmarkdown/html-document.html#code-folding
https://stackoverflow.com/questions/65655045
复制相似问题