如何在新键入的文本周围包含div元素,以便实时显示关联的css结果

时间:2015-06-08 06:21:31

标签: javascript jquery html

我创建了一个简单的文本编辑器,可以实时显示相关的CSS结果。例如,如果用户点击粗体,则在用户键入时文本变为粗体(显示实时更改)。同样如此,我想要一个按钮,点击后它会用带有指定类或id的div标签包装新文本。

我知道wrap方法可以包装div或其他标记。但是在通过wrap()添加新的div标签后,我希望存储包含新添加的div标签的结果。这样我就可以使用这些结果来生成新的html页面。

这是html代码:

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    <script src="texteditor.js"></script>
</head>
<body>
    <div id="content" style="margin-top: 10px; height: 70%; text-align: center;">
        <h2><u>Simple Text Editor Created Using jQuery</u></h2>
        <div class="ze ie"></div>
        <style>
            .font-bold.bold {
                font-weight: bold;
            }

            .italic {
                font-style: italic;
            }

            .selected {
                background-color: orange;
            }

            #openpb {
                margin: 15px;
            }
        </style>
        <button type="button" class="g-button g-button-submit" id='stext'>Text</button>&nbsp;&nbsp;&nbsp;&nbsp;
       <button type="button" class="g-button g-button-submit" id='shtml'>HTML</button>
        <div id="controls" style="margin-bottom: 10px;">
            <a id="bold" style="color: black; display: inline-block;" class="font-bold">
                <button type="button">B</button>
            </a>&nbsp;&nbsp;&nbsp;
        <a id="italic" style="color: black !important; display: inline-block;" class="italic">
            <button type="button">I</button>
        </a>&nbsp;&nbsp;&nbsp;&nbsp;
        <a id="link" class="link" style="display: inline-block;">
            <button type="button">Link</button>
        </a>&nbsp;&nbsp;&nbsp;&nbsp;
        <select id="fonts" class="g-button">
            <option value="Normal">Normal</option>
            <option value="Arial">Arial</option>
            <option value="Comic Sans MS">Comic Sans MS</option>
            <option value="Courier New">Courier New</option>
            <option value="Monotype Corsiva">Monotype</option>
            <option value="Tahoma New">Tahoma</option>
            <option value="Times">Times</option>
            <option value="Trebuchet New">Trebuchet</option>
            <option value="Ubuntu">Ubuntu</option>
        </select>
        </div>
        <iframe frameborder="0" id="textEditor" style="width: 500px; height: 80px; border: 2px solid #CCC; border-radius: 20px; overflow: auto;"></iframe>
        <textarea name="text" id='text' style="border-radius: 20px; overflow: auto; display: none; padding-left: 10px;" rows="6" cols="53"></textarea>
    </div>
</body>
</html>

这里是Jquery:我可以使用setinterval方法在textarea中实时显示结果。像这样我想要一个按钮,它将使用带有指定id和class的div标签包装新的或选定的文本,我希望在textarea中存储这些更改(新添加的div标签)。

$(document).ready(function () {
    document.getElementById('textEditor').contentWindow.document.designMode = "on";
    document.getElementById('textEditor').contentWindow.document.close();
    var edit = document.getElementById("textEditor").contentWindow;
    edit.focus();
    $("#bold").click(function () {
        if ($(this).hasClass("selected")) {
            $(this).removeClass("selected");
        } else {
            $(this).addClass("selected");
        }
        boldIt();
    });
    $("#italic").click(function () {
        if ($(this).hasClass("selected")) {
            $(this).removeClass("selected");
        } else {
            $(this).addClass("selected");
        }
        ItalicIt();
    });
    $("#fonts").on('change', function () {
        changeFont($("#fonts").val());
    });
    $("#link").click(function () {
        var urlp = prompt("What is the link:", "http://");
        url(urlp);
    });
    $("#stext").click(function () {
        $("#text").hide();
        $("#textEditor").show();
        $("#controls").show()
    });
    $("#shtml").on('click', function () {
        $("#text").css("display", "block");
        $("#textEditor").hide();
        $("#controls").hide();
    });
});
function boldIt() {
    var edit = document.getElementById("textEditor").contentWindow;
    edit.focus();
    edit.document.execCommand("bold", false, "");
    edit.focus();
}
function ItalicIt() {
    var edit = document.getElementById("textEditor").contentWindow;
    edit.focus();
    edit.document.execCommand("italic", false, "");
    edit.focus();
}
function changeFont(font) {
    var edit = document.getElementById("textEditor").contentWindow;
    edit.focus();
    edit.document.execCommand("FontName", false, font);
    edit.focus();
}
function url(url) {
    var edit = document.getElementById("textEditor").contentWindow;
    edit.focus();
    edit.document.execCommand("Createlink", false, url);
    edit.focus();
}
setInterval(function () {
    var gyt = $("#textEditor").contents().find("body").html().match(/@/g);
    if ($("#textEditor").contents().find("body").html().match(/@/g) >= 0) { } else {
        $("#text").val($("#textEditor").contents().find("body").html());
    }
    $("#text").val($("#textEditor").contents().find("body").html());
}, 1000);

我不想使用第三方文本编辑器插件。总之,我想要包含一个按钮的texteditor,点击这个按钮后,它将用div标签包装带有指定类或id的新文本。像Stack Overflow文本编辑器......可以添加带有指定类或id的自定义div。

1 个答案:

答案 0 :(得分:0)

<html>
    <head>
        <style>
            #Problem{margin: 5px; outline: 1px dotted red; padding: 5px}
            #Solution{margin: 5px; outline: 1px dotted green; padding: 5px}

            #Solution textarea{
                height: 200px;
                width: 500px;
            }
        </style>

        <script>
            //Wraps the text in a new div with optional id and classname.
            //Returns the new div.
            function wrapMe(text, id, classname){
                var tE = null;

                if (text && text.trim() != ''){
                    tE = document.createElement('div');
                    tE.id = (id || '');
                    tE.className = (classname || '');
                    tE.innerHTML = (text || '');
                }

                return tE
            }

            //Adds the outer html of passed element to the textarea with a linebreak
            function addElementToTextarea(e){
                var tA = document.querySelector('textarea');
                if (tA && e){
                    tA.innerHTML = (tA.innerHTML || '') + e.outerHTML + '&#013;&#010'
                }
            }
        </script>
    </head>

    <body>
        <div id = 'Problem'>
            Problem: I don't want use third party text editor plugins. in short i want texteditor which contain a button, after clicking this button it will wrap new text with div tag with specified class or id. something like stackover flow text editor...with facility of adding custom div with speicifed class or id.
        </div>

        <div id = 'Solution'>
            <input type = 'text' />
            <button onclick = "addElementToTextarea(wrapMe(this.parentNode.querySelector('input').value, 'testid', 'testclass'))">Wrap me</button>
            <br /><br />
            <textarea readonly = 'readonly'></textarea>
        </div>
    </body>
</html>