简体   繁体   English

如何将 append html 标记从一个 div 到另一个 div?

[英]How to append html tags from an div onto another div?

Is it possible to append html tags from a content-editable div to another div?是否可以将 append html 标签从内容可编辑的 div 转移到另一个 div? I wanted to make an html editor, and I wanted to make automatic preview.我想做一个 html 编辑器,我想做自动预览。

Here is the jsfiddle code: https://jsfiddle.net/eqw8L4to/12/这是jsfiddle代码: https://jsfiddle.net/eqw8L4to/12/

And here is the js code where I tried getting html tags from.html, and tried appending it to.result:这是我尝试从.html获取html标签的js代码,并尝试将其附加到.result:

$(document).ready(function() {
    $(".html").keydown(function(event) {
        var x = event.keyCode
        if (x == 27 && event.ctrlKey) {
            $(".result").html("")
            var y = $(".html").html()
            $(".result").append(y)
        }
    })
})

https://jsfiddle.net/cse_tushar/68na2mrq/8/ https://jsfiddle.net/cse_tushar/68na2mrq/8/

Get the text from contenteditable div and replace place in the result HTML从 contenteditable div 获取文本并替换结果 HTML 中的位置

$(document).ready(function() {
  const el = $("div.html");
  const result = $(".result");
  el.keydown(function(event) {
    if (event.keyCode === 27 && event.ctrlKey) {
      result.html(el.text());
    }
  })
})

在此处输入图像描述

You're pretty close.你很接近。 Use the .html method to provide the html instead.改用.html方法来提供 html。

Try this snippet and add the following to the test box and you'll see it is working.试试这个片段并将以下内容添加到测试框中,你会看到它正在工作。

<div class='rendered'>blue text</div>

 $(document).ready(function() { $(".html").keydown(function(event) { var x = event.keyCode if (x == 27 && event.ctrlKey) { $(".result").html("") var y = $(".html").text() $(".result").html(y) } }) })
 body{ margin:0; }.html{ color: white; height:50vh; width:100vw; background-color:#1a1a1a; }.result{ height:50vh; width:100vw; background-color:#f5ff5; }.rendered { color: blue; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="html" contenteditable="true"> </div> <div class="result"> </div>

You have to know which element your editor are using, but if div.html is the element that you want to get the text to preview, you just gotta remove double quotes and you'll get the content of element.您必须知道您的编辑器正在使用哪个元素,但如果div.html是您想要预览文本的元素,您只需删除双引号即可获得元素的内容。

Thy this:你这个:

$(document).ready(function() {
    $(".html").keydown(function(event) {
        var x = event.keyCode;
        if (x == 27 && event.ctrlKey) {
            $(".result").html();
            var y = $(".html").html();
            $(".result").append(y);
        }
    })
})

Well, as you've mentioned that you need an HTML editor with automatic preview, I tried it by reading the contenteditable div's input as text and then convert it to HTML using object.src = "data:text/html" with a default charset: UTF-8, and show it as the src of an iframe on keyup.好吧,正如您所提到的,您需要一个具有自动预览功能的 HTML 编辑器,我尝试通过将 contenteditable div 的输入作为文本读取,然后使用object.src = "data:text/html" : UTF-8,并在 keyup 上将其显示为 iframe 的 src。

I've read your code, but I am not very well-versed in JQuery...我已经阅读了您的代码,但我对 JQuery 不是很精通......

So, in standard the JavaScript it would be something like this:因此,在标准 JavaScript 中,它会是这样的:

const editor = document.querySelector(".editable-div");
const result = document.querySelector("iframe");

editor.addEventListener("keyup", () => {
  var html = editor.textContent;
  iframe.src = "data:text/html;charset=utf-8," + encodeURI(html);

Here's an example snippet:这是一个示例片段:

 const editor = document.querySelector(".editor"); const result = document.querySelector("iframe"); editor.addEventListener("keyup", () => { var html = editor.textContent; result.src = "data:text/html;charset=utf-8," + encodeURI(html); }); /*a paste function just as extra info*/ editor.addEventListener("paste", function(e) { e.preventDefault(); var text = e.clipboardData.getData("text/plain"); document.execCommand("insertText", false, text); });
 /* all CSS is for demo snippet */.editor { background-color: #000; height: 100px; width: 80vw; color: white; font: monospace; overflow-y: auto; white-space: pre; outline: none; }.result { background-color: rgb(255, 255, 255); width: 80vw; height: 100px; overflow-y: scroll; }
 <div class="editor" contenteditable>WriteCode</div> <iframe class="result"></iframe>

Here's an image of it working:这是它工作的图像:编辑工作的图像


Hope I could help.希望我能帮上忙。

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

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