Error: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

前端 未结 5 1614
孤街浪徒
孤街浪徒 2020-11-27 11:57

I am trying to drag and drop an image on a div. The image does not get get dragged onto the div and gives the following error

Uncaught TypeError: Failed to e         


        
相关标签:
5条回答
  • use ondragstart(event) instead of ondrag(event)

    0 讨论(0)
  • 2020-11-27 12:18

    It's actually a simple answer.

    Your function is returning a string rather than the div node. appendChild can only append a node.

    For example, if you try to appendChild the string:

    var z = '<p>test satu dua tiga</p>'; // is a string 
    document.body.appendChild(z);
    

    The above code will never work. What will work is:

    var z = document.createElement('p'); // is a node
    z.innerHTML = 'test satu dua tiga';
    document.body.appendChild(z);
    
    0 讨论(0)
  • 2020-11-27 12:19

    In my case, there was no string on which i was calling appendChild, the object i was passing on appendChild argument was wrong, it was an array and i had pass an element object, so i used divel.appendChild(childel[0]) instead of divel.appendChild(childel) and it worked. Hope it help someone.

    0 讨论(0)
  • 2020-11-27 12:25

    This can happen if you accidentally are not dragging the element that does have an id assigned (for example you are dragging the surrounding element). In that case the ID is empty and the function drag() is assigning an empty value which is then passed to drop() and fails there.

    Try assigning the ids to all of your elements, including the tds, divs, or whatever is around your draggable element.

    0 讨论(0)
  • 2020-11-27 12:25

    You may also use element.insertAdjacentHTML('beforeend', data);

    Please read the "Security considerations" on MDN.

    0 讨论(0)
提交回复
热议问题