2016-10-18 35 views
0

HTML文件:jQuery的:可放開不起作用

<!DOCTYPE html> <html> 
<head> 
    <meta charset="utf-8" /> 
    <title>Drag and drop</title> 
    <link rel="stylesheet" href="style.css">   
    <script src="jquery-3.1.1.min.js"></script> 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
    <script src="script.js"></script> 
</head> 
<body> 
    <ul> 
     <li> Milk </li> 
     <li> Bread </li> 
    </ul> 
    <div id ="list"></div> 
</body> </html> 

CSS文件,工作非常好: 「看起來你的帖子大多代碼,請添加一些更多的細節」

ul { padding: 0; list-style: none; color: red } #list { 
width: 450px; 
height: 450px; 
background-color: #f0f0f0; 
border: 1px solid #000; 
overflow: auto; } #list.border { 
border-width: 2px; } 

jQuery的文件:

$(document).ready(function() { $('li').draggable({containment: 'document', revert: true, start: function() 
{ 
    contents = $(this).text(); 
} 

}); $('#list').droppable({hoverClass: 'border', drop: function() 
{ 
    $('#list').append(contents + '<br />'); 
} 

}); }); 

功能「可棄」不行,我不能發現的bug。

回答

0

你真的應該試着保持代碼的可讀性,保持縮進等等。如果你沒有,你將有告示,說:

  • 你定義之外的$(document).ready() - 功能的droppable -Objects,是什麼原因造成的錯誤,並
  • 你閉上你的$(document).ready() - 功能兩次(有一個)};到多少你的代碼的末尾。

$(document).ready(function() { 
 
    $('li').draggable({ 
 
    containment: 'document', 
 
    revert: true, 
 
    start: function() { 
 
     contents = $(this).text(); 
 
    } 
 
    
 
    $('#list').droppable({ 
 
    hoverClass: 'border', 
 
    drop: function() { 
 
     $('#list').append(contents + '<br />'); 
 
    } 
 
    }); 
 
});

試試這個代碼,它可以工作。