单击带有 codemirror 的按钮时如何撤消选定/突出显示的文本

2024-03-13

我有一个简单的 codemirror 文本编辑器,正在使用 bootstrap 进行工作。我可以单击粗体和代码按钮“确定”,它会正确包装所选/突出显示的文本。

问题一:当文本包含在标签中时,可以说<b>something</b>如果我再次选择/突出显示它并单击相同的粗体按钮我该怎么办undo it在代码镜像中?

Here's CodePen 完整视图 http://codepen.io/riwakawebsitedesigns/full/mVmxpp/

Here's Codepen 代码视图 http://codepen.io/riwakawebsitedesigns/pen/mVmxpp

Script

<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('<?','&lt;?')
        .replace('?>', '?&gt;')
        .replace('<script>', '&lt;script&gt;')
        .replace('<script>', '&lt;/script&gt;')
        .replace('<div>', '&lt;div&gt;')
        .replace('</div>', '&lt;/div&gt;')

    });

    //$('#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>

我尝试过这个并且它有效:

    $('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>标签,如果确实如此,则将其删除。

代码笔链接 http://codepen.io/dingo_d/pen/gPRGGa

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单击带有 codemirror 的按钮时如何撤消选定/突出显示的文本 的相关文章

随机推荐

  • 无法使用 Selenium WebDriver 在 Chrome 中加载默认配置文件

    我正在使用 Selenium WebDriver 在需要 http 身份验证的页面上执行某些操作 我已经登录我的默认配置文件 但 selenium chromedriver 会自动为每次使用使用新的配置文件 因此我无法通过身份验证阶段 因此
  • libcurl 回调 w/c++ 类成员

    取自libcurl 编程教程 http curl haxx se libcurl c libcurl tutorial html在 libcurl 网站上 libcurl 与 C 使用 C 时基本上只需要记住一件事 在连接 libcurl
  • 未注入通过 RequireJS 从 CDN 加载 Angular

    在我的项目中 我想使用 RequireJS 并引导我的应用程序 如下所示 requirejs config baseUrl scripts vendor paths jquery https ajax googleapis com ajax
  • CustomAttribute反映html属性MVC5

    希望找到一种方法 当在 MVC5 中使用 Custom 属性或最好使用 RegularExpressionAttribute 装饰模型中的属性时 html 控件将包含它作为控件的另一个属性 例如 class CoolModel Custom
  • Hibernate 查询语言中四舍五入到小数点后两位

    你好 有人可以帮我如何在 hql 中四舍五入到小数点后两位吗 我在网上找不到任何东西 以下是我的查询 Select p amount as amt p desc from pay p register r where r type 1 an
  • 将字符串中匹配的正则表达式值替换为字典中的正确值

    我有一根绳子 var text the animal jumped over the description fox 和一本字典 var dictionary animal dog description jumped 我正在编写一个函数
  • CSS:100% 字体大小 - 100% 是什么?

    有many http www alistapart com articles howtosizetextincss articles http www w3 org TR CSS2 fonts html font size props an
  • java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener 错误

    您好 我的 struts1 spring 和 hibernate 集成中的动态 Web 应用程序是在 64 位计算机上开发的 它在 64 位计算机上运行良好 但在 32 位计算机上出现 jar 问题 并给出以下错误 SEVERE Error
  • 从 Google Chrome 自动生成 HAR 文件

    基本上我需要的是一种自动化的方法result以下操作 打开一个新选项卡 打开开发者工具中的网络选项卡 加载一个 URL 选择 全部另存为 HAR 通常 建议的解决方案涉及使用PhantomJS http phantomjs org brow
  • WAMP 的 MySQL 数据库文件驻留在哪里?

    我只是好奇 因为我开始学习 PHP 和 MySQL MySQL 的数据库和其他文件驻留在硬盘上的位置 我在 Windows XP SP2 平台上安装了 WAMP 从以下命令的输出中探索变量 mysql gt show variables l
  • Xamarin.Forms 中主从页面的母版页有多宽?

    根据屏幕尺寸 和设备习惯 母版页的宽度会有所不同 在手机上 它约为屏幕宽度的 80 而在平板电脑上 它似乎是一个恒定尺寸 如 320 dp 有人知道这个值的一般公式吗 我想用它在施工期间布置一些元素 当Width属性尚未设置 Edit I
  • 在 django 中使用 Context 时如何禁用 HTML 编码

    在我的 django 应用程序中 我使用模板来构建电子邮件正文 其中一个参数是 url 请注意 url 中有两个参数以 分隔 t loader get template sometemplate c Context foo bar url
  • ES6 Promise / Typescript 和 Bluebird Promise

    我有一个 nodejs typescript 2 项目并使用es6 promise https github com stefanpenner es6 promise包裹 现在我想摆脱额外的包 因为我可以直接在打字稿中定位 ES6 所以我删
  • Git和ssh授权

    我无法使用生成的 ssh 密钥登录 github 我已经按照本手册进行操作 http help github com linux key setup http help github com linux key setup但在步骤 ssh
  • 是否可以在任何现有的 onsubmit/submit 之前绑定一个 Submit() 函数?

    我有一个表格onsubmit属性 我需要绑定一个新的提交事件 并且需要在任何现有的提交函数之前执行该事件 下面的代码演示了这个问题
  • 嵌套砌体对象

    我正在尝试使用容器创建图形视图 因此 如果 A s gt B s gt C s 视图将显示 Bs 内的对象 c 而 Bs 又在 As 内 Something like this 我认为砌体视图非常适合此目的 但我无法使嵌套正常工作 到目前为
  • .NET Ionic.Zip:压缩或未压缩大小,或偏移量超过最大值

    我进行了以下设置 Win2008服务器 Ionic zip 参考模块 用于构建 zip 文件的单独驱动器 NET 4 0 Web 应用程序即时构建 zip 包 并允许客户端下载该包 该系统一直运行良好 直到现在 最近我们添加了一些较大的文件
  • git error“请告诉我你是谁。”和赫罗库

    我当时工作于branch master 并提交到 Git 存储库 一切都很好 我将新应用程序连接到 Heroku 上的这个存储库 我同时致力于 Heroku 和 Git 一切又恢复正常了 除了我无法在 Heroku 上运行 db migra
  • GAE - Python 3.7 - 如何登录?

    我有一个 python 3 7 中的谷歌应用程序引擎项目 我想在其中编写一些日志 我习惯在应用程序引擎 python 2 7 中编程 并且使用简单的代码 logging info hi there 将任何日志写入谷歌云日志控制台 上面的命令
  • 单击带有 codemirror 的按钮时如何撤消选定/突出显示的文本

    我有一个简单的 codemirror 文本编辑器 正在使用 bootstrap 进行工作 我可以单击粗体和代码按钮 确定 它会正确包装所选 突出显示的文本 问题一 当文本包含在标签中时 可以说 b something b 如果我再次选择 突