我正在使用以下HTML项目。 li标签位于引导选项卡结构中。我认为这与尺寸有关,因为我的可拖动类的宽度为200px,而li标签的宽度为100px。

   <li id="dropmessages1"><a href="#id" data-toggle="tab">Cat 1 </a></li>
   <li id="dropmessages2"><a href="#id" data-toggle="tab">Cat 2 </a></li>

   <div id="1" class="dragable"><a href="" class="list-group-item"></a></div>
   <div id="2" class="dragable"><a href="" class="list-group-item"></a></div>


和下面的jQuery代码。

   $( init );

   function init() {
   $('.dragable').draggable({
   cursor: 'move',
   revert: true
   });

   $('#dropmessages1').droppable({
   drop: handleDropEvent
   } );

   $('#dropmessages2').droppable({
   drop: handleDropEvent
   } );
   }


  function handleDropEvent( event, ui ) {

  var draggable = ui.draggable;
  alert( 'The message  with ID "' + draggable.attr('id') + '" was dropped onto me!' );
  }


我可以拖动“可拖动”项,但不能将它们拖放到LI项上。是否可以将物品放到li标签上?

最佳答案

嗨,检查以下代码:

   <!DOCTYPE HTML>
<html>
<head>
<style>
#div1 {width:350px;height:70px;padding:10px;border:1px solid #aaaaaa;}
</style>
<script>
function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>

<p>Drag the W3Schools image into the rectangle:</p>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<img id="drag1" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69">

</body>
</html>

关于css - 如何使用jQuery拖放到li标签上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34544598/

10-13 01:50
查看更多