这是我的代码摘自http://jqueryui.com/demos/draggable/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Draggable - Default functionality</title>
<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
<script src="../../jquery-1.6.2.js"></script>
<script src="../../ui/jquery.ui.core.js"></script>
<script src="../../ui/jquery.ui.widget.js"></script>
<script src="../../ui/jquery.ui.mouse.js"></script>
<script src="../../ui/jquery.ui.draggable.js"></script>
<link rel="stylesheet" href="../demos.css">
<style>
.draggable { width: 150px; height: 150px; padding: 0.5em; }
</style>
<script>
$(function() {
$( ".draggable" ).draggable();
$('.content').click(function(){
var htmlData='<div class="draggable ui-widget-content ui-draggable"><p>Drag me around</p></div>';
$('.demo').append(htmlData);
});
});
</script>
</head>
<body>
<div class="demo">
<div class="draggable ui-widget-content">
<p>Drag me around</p>
</div>
<div class="content">Test </div>
</div><!-- End demo -->
</body>
</html>正如您在函数Iam中使用append所看到的,iam正在动态地使组件可拖动。但是新生成的drggable对象不能拖动,尽管我已经给出了jquery-ui生成的类。
发布于 2011-10-05 19:37:48
添加动态创建的元素后,尝试调用$( ".draggable" ).draggable();。
$(function() {
$( ".draggable" ).draggable();
$('.content').click(function(){
var htmlData='<div class="draggable ui-widget-content ui-draggable"><p>Drag me around</p></div>';
$('.demo').append(htmlData);
$( ".draggable" ).draggable();
});
});发布于 2012-01-18 09:11:28
实际上,出于性能原因,我将使用:
$('.draggable:not(.ui-draggable)').draggable();这将防止尝试使用jQuery UI的内置类重新初始化已经可拖动的元素。此外,如果您的代码修改了单个可拖动对象的属性,而该属性可能与新可拖动对象的属性不匹配,则最终可能会重置一些可拖动对象。
具体使用jQuery并没有什么错。
更新
没有指定哪个实例使用它。基于Ricardo的编辑:
$(function() {
$( ".draggable" ).draggable();
$('.content').click(function(){
var htmlData='<div class="draggable ui-widget-content"><p>Drag me around</p></div>';
$('.demo').append(htmlData);
$('.draggable:not(.ui-draggable)').draggable(); //Here
});
});我现在还看到您手动添加了ui-draggable类。你不需要这样做。事实上,它使我所说的不起作用。
发布于 2012-01-16 01:49:32
<script>
$(function() {
$(".draggable").draggable();
$(".content").click(function(){
var htmlData='<div class="draggable ui-widget-content ui-draggable"><p>Drag me around</p></div>';
$(".demo").append(htmlData);
});
});
</script>只需要改变位置就行了
<script>
$(function() {
$(".content").click(function(){
var htmlData='<div class="draggable ui-widget-content ui-draggable"><p>Drag me around</p></div>';
$(".demo").append(htmlData);
$(".draggable").draggable(); //bring it here so that for the newly inserted/created dom elements, jquery draggable can be initialized.
});
});
</script>https://stackoverflow.com/questions/7660606
复制相似问题