我不明白为什么我有这个错误:TypeError: ui是未定义的
然而,代码非常简单!演示代码: http://jsfiddle.net/9sNEK/19/
最后一个目标是,当我拖动绿色div时,它会触发红色div的拖动。
JS
$('.div_green')
.draggable()
.bind('drag',function(event,ui){
$('.div_red').trigger('drag');
$('.log').html("drag green - left:" + ui.position.left);
});
$('.div_red')
.draggable()
.bind('drag', function(event,ui){
$('.log2').html("drag_to_simulate - left:"+ ui.position.left);
});<div class="div_green"></div>
<div class="pdrag" id="pdrag1">
<div class="div_red"></div>
</div>
<div class="pdrag" id="pdrag2">
<div class="div_red"></div>
</div>
<div class="log" style="width:200px;height:20px;"></div>
<div class="log2" style="width:200px;height:20px;"></div>CSS
.div_green, .pdrag, .div_red{
position:absolute;
}
.div_green{
width:40px;
height:40px;
top:170px;
left:300px;
background-color: green;
}
.pdrag{
width:120px;
height:120px;
top:50px;
left:90px;
background-color: #F1F1F1;
}
#pdrag2{
top:230px;
width:150px;
}
.div_red{
width:20px;
height:20px;
top:40px;
left:50px;
background-color:red;
}
#pdrag2 .div_red{
left:75px
}非常感谢你的帮助。
发布于 2012-07-23 12:09:47
您获得TypeError: ui is undefined是因为您在..div_green的拖动绑定中触发了.div_red的拖动事件。
$('.div_red').trigger('drag');但没有提供ui的价值,
$('.div_green')
.draggable()
.bind('drag',function(event,ui){
//* $('.div_red').trigger('drag'); // ui is missing, you should provide the ui here
* $('.div_red').trigger('drag', ui); // passing ui here
$('.log').html("drag green - left:" + ui.position.left);
});检查语句从*开始
或者您可以通过$(this).data('draggable')获得元素的ui,您可以更改.div_red的代码,如
$('.div_red')
.draggable()
.bind('drag', function(event,ui){
if(ui != null)
$('.log2').html("drag_to_simulate - left:"+ ui.position.left);
else
$('.log2').html("drag_to_simulate - left:"+ $(this).data('draggable').position.left);
});发布于 2014-02-03 17:23:35
我认为你的标签顺序是错的!
我也有过同样的问题.
例如:
<script src="<?php echo site_url('js/jquery.mjs.nestedSortable.js'); ?>"></script>
<script src="<?php echo site_url('js/jquery-ui-1.10.4.custom.min.js'); ?>"></script>然后换了他们..。然后砰!!就是这样!!希望这会有帮助!
https://stackoverflow.com/questions/11612094
复制相似问题