ComponentOne Wijmo Editor 在光标处添加文本

发布时间:2012/11/16 00:11 发布者:葡萄城产品团队

返回博客中心

ComponentOne Wijmo Editor 是一款强大的HTML 编辑器,可以插入table, image, links 等标签,但是没有在光标处直接插入标签的功能。但是,现在Wijmo Editor 是一款基于 jQuery 控件。现在们可以通过客户端 object 模型去实现。
在本文中,我们将通过自定义方法来实现该功能。
首先,我们要插入wijmo:C1Editor 标签来展示 Wijmo Editor 。在使用 iFrame 加载Wijmo Editor 之后,我们就可以使用 createRange 和 execCommand 方法来实现在光标处插入文本了。
下面是实现该功能的完整代码:

 
<script type="text/javascript">
  
function insetTextAtCursor(inputText) {
    //get the container
    var $designView = $("iframe", $(".wijmo-wijeditor-container")),
         win, doc, range;
  
    if ($designView && $designView.length > 0) {
        //retrieve the Window object generated by the iframe
        win = $designView[0].contentWindow;
     }

    if (win) {
         //access the document object
         doc = win.document;
     }
  
     if (doc) {
        try {
           //check if the browser is IE
           if ($.browser.msie) {
              //insert the given text
              doc.body.focus();
              range = doc.selection.createRange();
              range.pasteHTML(inputText);
              range.collapse(false);
              range.select();
           } else {
              doc.execCommand('insertText', false, inputText);
           }
        } catch (e) {
     }
   }
}
</script>
复制代码

截图展示:

Demo下载:

C1WijEditor_InsertText.zip (129.75 K, 下载次数:29)

关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网