$()函数无法正常工作

时间:2017-08-23 14:12:47

标签: javascript jquery

function drag_drop(event) {
    event.preventDefault(); /* Prevent undesirable default behavior while dropping */
    var elem_id = event.dataTransfer.getData("text");
    event.target.appendChild( document.getElementById(elem_id) );
    $('#app_status').innerHTML = "Dropped "+elem_id+" into the "+event.target.getAttribute('id');
    document.getElementById(elem_id).removeAttribute("draggable");
    document.getElementById(elem_id).style.cursor = "default";
    droppedIn = true;
}

这个功能运行正常但是当我使用jQuery的$()方法而不是document.getElementById更新它时,它会出错:

index.js:22 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
    at drag_drop (index.js:22)
    at HTMLDivElement.ondrop (index.html?_ijt=ifai0ih42qnjkillablo2ulcln:15)

更新的功能如下所示:

function drag_drop(event) {
    event.preventDefault(); /* Prevent undesirable default behavior while dropping */
    var elem_id = event.dataTransfer.getData("text");
    event.target.appendChild( $('#' + elem_id) );
    $('#app_status').innerHTML = "Dropped "+elem_id+" into the "+event.target.getAttribute('id');
    $('#' + elem_id).removeAttribute("draggable");
    $('#' + elem_id).style.cursor = "default";
    droppedIn = true;
}

1 个答案:

答案 0 :(得分:0)

根据jQuery

,您的代码几乎没有变化
function drag_drop(event) {
   event.preventDefault(); /* Prevent undesirable default behavior while dropping */
   var elem_id = event.dataTransfer.getData("text");
   event.target.appendChild( $('#' + elem_id) );

   // ---- **Updated Code**
   $('#app_status').html("Dropped "+elem_id+" into the "+event.target.getAttribute('id'));
   $('#' + elem_id).removeAttr("draggable");
   $('#' + elem_id).css({cursor: "default"});


   droppedIn = true;
}