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

Javascript 如何将div元素包装在新键入的文本周围,以便实时显示相关的css结果,javascript,jquery,html,Javascript,Jquery,Html,我创建了一个简单的文本编辑器,实时显示相关的CSS结果。例如,如果用户单击“加粗”,则文本在用户键入时变为加粗(显示实时更改)。与此相同,我希望在单击后有一个按钮,它将使用具有指定类或id的div标记包装新文本 <html> <head> <style> #Problem{margin: 5px; outline: 1px dotted red; padding: 5px} #Soluti

我创建了一个简单的文本编辑器,实时显示相关的CSS结果。例如,如果用户单击“加粗”,则文本在用户键入时变为加粗(显示实时更改)。与此相同,我希望在单击后有一个按钮,它将使用具有指定类或id的div标记包装新文本

<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>
我知道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>
我不想使用第三方文本编辑器插件。简而言之,我想要一个包含按钮的文本编辑器,单击此按钮后,它将使用具有指定类或id的div标记包装新文本。类似于堆栈溢出文本编辑器…具有添加具有指定类或id的自定义div的功能。


<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>
#问题{margin:5px;outline:1px点红色;padding:5px} #解决方案{边距:5px;轮廓:1px绿色点;填充:5px} #解决方案文本区域{ 高度:200px; 宽度:500px; } //将文本包装到具有可选id和类名的新div中。 //返回新的div。 函数wrapMe(文本、id、类名){ var tE=null; if(text&&text.trim()!=“”){ tE=document.createElement('div'); tE.id=(id | |“”); tE.className=(className | |“”); tE.innerHTML=(文本| |“”); } 返回tE } //使用换行符将传递元素的外部html添加到textarea 函数加法器到ExtArea(e){ var tA=document.querySelector('textarea'); 如果(tA&e){ tA.innerHTML=(tA.innerHTML | |“”)+e.outerHTML+' ; ' } } 问题:我不想使用第三方文本编辑器插件。简而言之,我想要包含一个按钮的文本编辑器,点击这个按钮后,它将用指定类或id的div标记包装新文本。类似于stackover flow文本编辑器…具有添加带有特定类或id的自定义div的功能。 将我包裹


根据您的要求,我建议使用oninput而不是interval。我如何存储wrap的输出显示我可以在TextArea中显示新添加的文本您可以给出一些示例,如果可能的话,添加简单的div标记并在TextArea中显示其输出wrap()的输出就是wrap()的返回。console.log($('test').wrap(“”.parent().get(0).outerHTML)谢谢@Lain的帮助,这是我所需要的。不客气。如果你没有更多的问题,请记住接受它。你的意思是投赞成票,我没有特权,yetNo,接受它。所以其他人认为这个话题已经结束了,不要把时间浪费在解决问题上。答案旁边的绿色箭头。是的,我刚做了,有点新。谢谢:D@Lain