Uncaught TypeError: Cannot read property 'parentNode' of null javascript - Stack Overflow

This code should make an element disappear from a list when it is dropped on another element (called co

This code should make an element disappear from a list when it is dropped on another element (called cookiemonster)

addEvent(cookiemonster, 'drop', function (e) {
  if (e.stopPropagation) e.stopPropagation();

  var el = document.getElementById(e.dataTransfer.getData('text'));
  el.parentNode.removeChild(el);
  return false;
});

This code should make an element disappear from a list when it is dropped on another element (called cookiemonster)

addEvent(cookiemonster, 'drop', function (e) {
  if (e.stopPropagation) e.stopPropagation();

  var el = document.getElementById(e.dataTransfer.getData('text'));
  el.parentNode.removeChild(el);
  return false;
});
Share Improve this question edited Mar 26, 2014 at 23:43 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Aug 23, 2013 at 16:13 KepedizerKepedizer 8562 gold badges10 silver badges25 bronze badges 3
  • 1 This means that el is null.... – Naftali Commented Aug 23, 2013 at 16:14
  • what is the output of e.dataTransfer.getData('text')? there seems to be no element by that ID – Harry Commented Aug 23, 2013 at 16:14
  • It seems there is no element returned by the document.getElementById(); what does e.dataTransfer.getData('text') return? Does it match an id of an element in the document? – David Thomas Commented Aug 23, 2013 at 16:15
Add a ment  | 

1 Answer 1

Reset to default 1

create an auxiliar variable like this

addEvent(cookiemonster, 'drop', function (e) {
    if (e.stopPropagation) e.stopPropagation();

    var el = document.getElementById(e.dataTransfer.getData('text'));
    var aux = el.parentNode;
    aux.removeChild(el);
    return false;
});

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>