http://jsbin.com/EzaKuXE/1/edit
我一直在尝试将主题从默认更改为钴,等等,每次单击按钮时,它都会切换,但它只切换一次主题,而不会切换回。所以我才来这里。有人能帮助引导正确的方向吗?这个简单的任务越来越烦人了。
我用.(点)替换了代码中不相关的部分。
<link rel="stylesheet" href=http://codemirror.net/doc/docs.css>
.
.
<link rel="stylesheet" href="http://codemirror.net/theme/3024-night.css">
.
.
<link rel="stylesheet" href="http://codemirror.net/theme/neat.css">
</head>
<body>
<textarea id=code name=code>
.
.
<iframe id=preview></iframe>
<p>Select a theme: <select onchange="selectTheme()" id=select>
<option value="0" selected>default</option>
.
.
<option>night</option>
</select></p>
<button id="changetheme" style="margin-top: -35px; float: right;">Cobalt</button>
</body>
</html>Javascript
// Initialize CodeMirror editor with a nice html5 canvas demo.
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true
});
// Sets the theme
var input = document.getElementById("select");
function selectTheme() {
var theme = input.options[input.selectedIndex].innerHTML;
editor.setOption("theme", theme);
}
$(function() {
$('#changetheme').click(themechange);
function themechange() {
if (
$("#select").val(0)) {
$("#select").val(7).trigger('change');
}
else if (
$("#select").val(7)) {
$("#select").val(0).trigger('change');
}
}
});发布于 2013-09-01 17:51:48
对于使用jQuery切换内容,请使用toggleClass()函数。
http://api.jquery.com/toggleClass/
$(element).toggleClass("className");https://stackoverflow.com/questions/18561188
复制相似问题