我有一个简单的codemirror文本编辑器,我正在使用bootstrap。我可以单击我的粗体和代码按钮确定,它会严格包装所选/突出显示的文本。
问题1:当文字被包装在标签中时,我们可以说<b>something</b>
如果我再次选择/突出显示它并点击相同的粗体按钮,我该如何撤消它< / strong>在codemirror中?
脚本
<script type="text/javascript">
$(document).ready(function() {
// tooltips on hover
$('[data-toggle=\'tooltip\']').tooltip({container: 'body', html: true});
// Makes tooltips work on ajax generated content
$(document).ajaxStop(function() {
$('[data-toggle=\'tooltip\']').tooltip({container: 'body'});
});
$('[data-toggle=\'tooltip\']').on('remove', function() {
$(this).tooltip('destroy');
});
var editor = document.getElementById('text-editor');
$("#text-editor").each(function (i) {
editor = CodeMirror.fromTextArea(this, {
lineNumbers: true,
mode: 'html'
});
editor.on("change", function() {
document.getElementById('question-preview').innerHTML = editor.getValue('<br>')
.replace('<?','<?')
.replace('?>', '?>')
.replace('<script>', '<script>')
.replace('<script>', '</script>')
.replace('<div>', '<div>')
.replace('</div>', '</div>')
});
//$('#hr').append('<hr />');
$('a[role="button"]').click(function(){
var val = $(this).data('val');
var string = editor.getSelection();
switch(val){
case 'bold':
editor.replaceSelection('<b>' + string + '</b>');
break;
case 'italic':
editor.replaceSelection('<i>' + string + '</i>');
break;
case 'quote':
editor.replaceSelection('<blockquote><p>' + string + '</p></blockquote>');
break;
case 'code':
editor.replaceSelection('<pre><code>' + string + '</code></pre>');
break;
case 'hr':
editor.replaceSelection('<hr/>');
break;
}
});
$(".dropdown-menu li a[class='btn-heading']").click(function(){
var val = $(this).data('val');
var string = editor.getSelection();
switch(val){
case 'h1':
editor.replaceSelection('<h1>' + string + '</h1>');
break;
case 'h2':
editor.replaceSelection('<h2>' + string + '</h2>');
break;
case 'h3':
editor.replaceSelection('<h3>' + string + '</h3>');
break;
case 'h4':
editor.replaceSelection('<h4>' + string + '</h4>');
break;
case 'h5':
editor.replaceSelection('<h5>' + string + '</h5>');
break;
case 'h6':
editor.replaceSelection('<h6>' + string + '</h6>');
break;
}
});
});
});
</script>
HTML
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="panel panel-default" onLoad="text_editor();">
<div class="panel-heading">
<ul class="nav nav-pills" id="heading">
<li><a role="button" data-val="bold">Bold</a></li>
<li><a role="button" data-val="italic">Italic</a></li>
<li><a role="button" data-val="link">Hyperlink</a></li>
<li><a role="button" data-val="quote">Quote</a></li>
<li><a role="button" data-val="code">Code</a></li>
<li><a role="button" data-val="image">Image</a></li>
<li class="dropdown">
<a class="dropdown-toggle" aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown">Heading <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a class="btn-heading" data-val="h1">H1</a></li>
<li><a class="btn-heading" data-val="h2">H2</a></li>
<li><a class="btn-heading" data-val="h3">H3</a></li>
<li><a class="btn-heading" data-val="h4">H4</a></li>
<li><a class="btn-heading" data-val="h5">H5</a></li>
<li><a class="btn-heading" data-val="h6">H6</a></li>
</ul>
</li>
</ul>
</div>
<div class="panel-body">
<textarea id="text-editor" name="text-editor" class="form-control"></textarea>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div id="question-preview"></div>
</div>
</div>
</div>
答案 0 :(得分:2)
我试过这个并且它有效:
$('a[role="button"]').click(function(){
var val = $(this).data('val');
var string = editor.getSelection();
switch(val){
case 'bold':
if(string.indexOf('<b>') > -1){
editor.replaceSelection(string.replace('<b>', '').replace('</b>', ''));
} else{
editor.replaceSelection('<b>' + string + '</b>');
}
break;
case 'italic':
editor.replaceSelection('<i>' + string + '</i>');
break;
case 'quote':
editor.replaceSelection('<blockquote><p>' + string + '</p></blockquote>');
break;
case 'code':
editor.replaceSelection('<pre><code>' + string + '</code></pre>');
break;
case 'hr':
editor.replaceSelection('<hr/>');
break;
}
});
它只是检查你的字符串是否有<b>
标记,如果它只是删除它们。
答案 1 :(得分:1)