简体   繁体   English

JavaScript WYSIWYG富文本编辑器

[英]JavaScript WYSIWYG Rich Text Editor

What is the basic method of styling text inside input boxes? 在输入框内设置文本样式的基本方法是什么? Can you show me the most simple example of how to change text color separately? 你能告诉我一个如何分别改变文字颜色的最简单的例子吗?

Very simply: 非常简单:

window.document.designMode = “On”;

document.execCommand(‘ForeColor’, false, ‘red’);

For more detail: 有关详细信息:

http://shakthydoss.com/javascript/html-rich-text-editor/ http://shakthydoss.com/javascript/html-rich-text-editor/

and then 然后

http://shakthydoss.com/javascript/stxe-html-rich-text-editor/ http://shakthydoss.com/javascript/stxe-html-rich-text-editor/

There are 4 approaches you can adopt 您可以采用4种方法

  1. Create a textarea. 创建一个textarea。 Let user modify the content. 让用户修改内容。 On key press or button click insertHtml to preview div. 在按键或按钮上单击insertHtml以预览div。 Trix uses the same approach but programmatically. Trix使用相同的方法,但以编程方式。
  2. Add some textarea. 添加一些textarea。 Use some markdown processor which can render the the content of textarea. 使用一些可以渲染textarea内容的降价处理器。
  3. Handle every movement of blinking cursor and implement something like Google docs which doesn't use execCommands. 处理闪烁光标的每个动作,并实现不使用execCommands的Google文档。 I believe quilljs also doesn't use execCommands. 我相信quilljs也不使用execCommands。
  4. You can use execCommands which are supported by almost all modern browsers. 您可以使用几乎所有现代浏览器都支持的execCommands。 Here is the simplest example . 这是最简单的例子 Or check this example which uses this small code to run set of execCommands to make a rich text editor. 或者查看示例, 示例使用此小代码运行一组execCommands以创建富文本编辑器。 You can simplify it more. 您可以进一步简化它。

Example

angular.module("myApp", [])
    .directive("click", function () {
        return {
            restrict: "A",
            link: function (scope, element, attrs) {
                element.bind("click", function () {
                    scope.$evalAsync(attrs.click);
                });
            }
        };
    })
    .controller("Example", function ($scope) {
        $scope.supported = function (cmd) {
            var css = !!document.queryCommandSupported(cmd.cmd) ? "btn-succes" : "btn-error"
            return css
        };
        $scope.icon = function (cmd) {
            return (typeof cmd.icon !== "undefined") ? "fa fa-" + cmd.icon : "";
        };
        $scope.doCommand = function (cmd) {
            if ($scope.supported(cmd) === "btn-error") {
                alert("execCommand(“" + cmd.cmd + "”)\nis not supported in your browser");
                return;
            }
            val = (typeof cmd.val !== "undefined") ? prompt("Value for " + cmd.cmd + "?", cmd.val) : "";
            document.execCommand(cmd.cmd, false, (cmd.val || ""));
        }
        $scope.commands = commands;
        $scope.tags = [
    'Bootstrap', 'AngularJS', 'execCommand'
  ]
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM