【问题标题】:Jquery - i have problem with drag and drop?Jquery - 我有拖放问题?
【发布时间】:2011-07-18 12:14:46
【问题描述】:

我已经使用 jquery 完成了拖放操作。这里我有需要将拖动内容(teacher1)放到特定区域的问题。

jquery:

$(document).ready(function() {  

    //var a ='1';
    $(".list").draggable({
    helper: 'clone', 
    cursor: 'hand', 
    revert: true,
    drag: function(ev, ui) {    
        dragId = $(this).attr('id');        
        //alert(dragId);
/*      if (dragId==1) {
            dragClass = "drop1";
        }   
*/     /*else {
         alert('Hi');
       }*/
     }   
    });
    $(".drop1").droppable({
        accept: ".list",
        activeClass: 'drop-active',
        hoverClass: 'dropareahover',
        drop: function(ev, ui){
        //var dropDragId = ui.draggable.attr('id');
        //alert(dropDragId)
        var targetId = $(this).attr("id");
        $("#" + targetId).each(function() {         
        $(this).append(ui.draggable.text());                                          
       });
    },
    destroy: function(ev, ui) {
    }
 });      
});
</script>

HTML:

<div class="draggable">
<ul>
<li class="list" id="1">Teacher1</li>
<li class="list" id="2">Teacher2</li>
<li class="list" id="3">Teacher3</li>
<li class="list" id="4">Teacher4</li>
</ul>
</div>
<div class="drop">
<table class="tble">
<tr>
    <td id="td1" class="drop1 br">Vocational</td>
    <td id="td2" class="drop br">English<br /></td>
    <td id="td3" class="drop1 br">Mathematics<br /></td>
    <td id="td4" class="drop br">French<br /></td>
</tr>
<tr>
    <td id="td1a" class="drop br">Hindi</td>
    <td id="td2a" class="drop br">Science<br /></td>
    <td id="td3a" class="drop br">PTE<br /></td>
    <td id="td4a" class="drop br">Social Science<br /></td>
</tr>
</table>
</div>

css:

<style type="text/css">
.draggable {
  width:400px;
  height:auto;
  border:solid 1px #ccc;
}
.dropareahover {
  background-color:#EFD2A4;
  border-color:#DFA853;
}
.drop-active {
 background: #ffff99;
}

.draggable ul {
margin:0px;
padding:0px;
}
.draggable ul li {
list-style-type:none;
}
.drop {
width:400px;
height:auto;
margin:10px 0px 0px 0px;
border:solid 1px #ccc;
}
.drop1 {
border:solid 1px #CCCCCC;
width:100px;
}
.drop ul {
margin:0px;
padding:0px;
}
.drop ul li {
list-style-type:none;
}
.tble{border-collapse:collapse;}
.tble td{text-align:center;}
</style>

我需要放置拖动内容(teacher1)并放入职业和印地语,但不是数学。再次拖动teacher2内容并放入英文,而不是其他单元格。

我做了一些工作。但我无法将teacher2 放到特定区域。请帮帮我。

【问题讨论】:

  • 我为你创建了一个 Fiddle,让测试这类问题变得更加容易。 jsfiddle.net/tRhnC/1
  • 感谢您为我创造了小提琴...:-))

标签: javascript jquery jquery-ui drag-and-drop jquery-ui-draggable


【解决方案1】:

据我了解,您需要为每个过滤器创建不同的放置区域

使用http://jqueryui.com/demos/droppable/#accepted-elements,您可以创建 4 个放置区域,每个区域都接受一个特殊的类。

hindu 只接受带有“.hindu”的那些

PTE 只接受带有“.PTE”的人

希望对你有帮助

【讨论】:

  • 是的,你说得对,我只提到了4位老师,但是老师在这个意义上增加了更多,我必须写同样的方式或者有没有其他方法来简化
  • 假设你所有的放置区域都有相同的类 '.drop' 并且每个都有一个特殊的类,比如 '.PTEdrop' 或 '.hinduDrop' 你可以设置它们所有的放置属性共同点然后做类似的事情:code$(".hindu").droppable("option", "accept", '.hindu');对于每个放置区域。 (这很简单:))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 2011-02-25
相关资源
最近更新 更多