javascript - cannot append parsed string to dom - Stack Overflow

I'm using DOMParser toparse a string with and html tag to append it on another dom nodewindow.ad

I'm using DOMParser to parse a string with and html tag to append it on another dom node

  window.addEventListener("load",carga);
  var origen = document.getElementById('origen');
  var destino = document.getElementById('destino');

  function carga(e){
    origen.addEventListener('dragstart',function(e){
        e.dataTransfer.setData("Text",origen.outerHTML);
    },false);

    destino.addEventListener('dragover',function(e){
        e.preventDefault();
    });


    destino.addEventListener('drop',function(e){
        e.preventDefault();
        console.log( e.dataTransfer.getData("Text"));
        var parser = new DOMParser();
        dragged = parser.parseFromString( e.dataTransfer.getData("Text") , "text/html");
        console.log(dragged);
        destino.appendChild(dragged);
    },false);
  }

The content of dragged variable is :

<section draggable="true" id="origen" style="height: 50px; width: 50px; border-color: green; border-style: solid;">origen</section>

I'm using DOMParser to parse a string with and html tag to append it on another dom node

  window.addEventListener("load",carga);
  var origen = document.getElementById('origen');
  var destino = document.getElementById('destino');

  function carga(e){
    origen.addEventListener('dragstart',function(e){
        e.dataTransfer.setData("Text",origen.outerHTML);
    },false);

    destino.addEventListener('dragover',function(e){
        e.preventDefault();
    });


    destino.addEventListener('drop',function(e){
        e.preventDefault();
        console.log( e.dataTransfer.getData("Text"));
        var parser = new DOMParser();
        dragged = parser.parseFromString( e.dataTransfer.getData("Text") , "text/html");
        console.log(dragged);
        destino.appendChild(dragged);
    },false);
  }

The content of dragged variable is :

<section draggable="true" id="origen" style="height: 50px; width: 50px; border-color: green; border-style: solid;">origen</section>
Share Improve this question edited Nov 10, 2017 at 9:01 cнŝdk 32.2k7 gold badges60 silver badges80 bronze badges asked Nov 10, 2017 at 8:13 af_imi4af_imi4 911 silver badge7 bronze badges 4
  • Any errors in the console? You can also append HTML as string without a DOMParser with .insertAdjacentHTML() – Andreas Commented Nov 10, 2017 at 8:16
  • the console error is TypeError: Argument 1 of Node.appendChild is not an object – af_imi4 Commented Nov 10, 2017 at 8:18
  • Try var dragged = parser.parseFromString(e.dataTransfer.getData("Text") , "text/html").body.firstElementChild. – dfsq Commented Nov 10, 2017 at 8:31
  • @af_imi4 I just gave you the explanation in my answer, did you checked it? – cнŝdk Commented Nov 10, 2017 at 10:13
Add a ment  | 

1 Answer 1

Reset to default 7

Actually when you use DOMParser's parseFromString() method with the option text/html it will return a HTMLDocument and not a DOM element. So you can't directly append the result to an element, that's why you got TypeError: Argument 1 of Node.appendChild is not an object.

You need to get the content of this HTMLDocument before appending it to your Element, this is how you should implement it:

var parser = new DOMParser();
var dragged = parser.parseFromString( "<div><p>foo</p><p>bar</p></div>" , "text/html");

var divDoc = dragged.getRootNode();
destino.appendChild(divDoc.body);

Demo:

This is a working Example:

var parser = new DOMParser();
var dragged = parser.parseFromString( "<div><p>foo</p><p>bar</p></div>" , "text/html");

var divDoc = dragged.getRootNode();
document.body.appendChild(divDoc.body);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745246107a4618418.html

相关推荐

  • javascript - cannot append parsed string to dom - Stack Overflow

    I'm using DOMParser toparse a string with and html tag to append it on another dom nodewindow.ad

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信