gpt4 book ai didi

javascript - CodeMirror:从按钮折叠全部或展开全部?

转载 作者:行者123 更新时间:2023-12-04 03:49:11 50 4
gpt4 key购买 nike

我想在 codemirror textarea 周围设置按钮,允许用户通过单击按钮展开全部或全部折叠。这将避免他们不得不学习捷径。可能吗?

这是编辑器的定义(extraKeys 的功能符合预期):

    var editor = CodeMirror.fromTextArea(document.getElementById("xmlViewer"), {
mode: "text/html",
lineNumbers: true,
tabMode: 'indent',
lineWrapping: true,
autoCloseTags: true,
height: "650px",
width: "90%",
parserfile: "parsexml.js",
stylesheet: "css/xmlcolors.css",
path: "js/",
continuousScanning: 500,
extraKeys: {
"Ctrl-Q": function (cm) { cm.foldCode(cm.getCursor()); },
"Alt-F": "findPersistent",
"Ctrl-Y": "foldAll",
"Ctrl-I": "unfoldAll"
},
foldGutter: true,
autoRefresh: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
});

这是按钮的定义:

<input type="button" id="expandAllBtn" value="Expand All" class="btn btn-outline-primary btn-sm" />

这里是 onclick 的定义:

$("#collapseAllBtn").on("click", function (e) {

editor.foldAll; // nothing happens
//editor.foldAll(); // error not a function
//editor.commands.foldAll(); // cannot read property of undefined
//editor.commands.foldAll; // cannot read property of undefined
});

最佳答案

下面是实现这个的方法:

$("#collapseAllBtn").on("click", function (e) {

editor.execCommand("foldAll");

});

关于javascript - CodeMirror:从按钮折叠全部或展开全部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64670121/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com