未捕获的TypeError:无法读取null javascript的属性'parentNode'

时间:2013-08-23 16:13:23

标签: javascript

当一个元素被删除到另一个元素(称为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;
});

1 个答案:

答案 0 :(得分:1)

像这样创建一个辅助变量

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;
});