简体   繁体   English

新创建的dom元素不可拖动

[英]newly created dom elements cannot be draggable

I'm using jquery and jquery ui to create draggable elements. 我正在使用jquery和jquery ui创建可拖动元素。 i created a small jsfiddle example at https://jsfiddle.net/2j2c8vLc/ that demonstrates what i'm trying to do. 我在https://jsfiddle.net/2j2c8vLc/上创建了一个小jsfiddle示例,该示例演示了我正在尝试做的事情。

javascript code: JavaScript代码:

$(function(){
    $('body').append('<div id="moshe">hi</div>');
    $('#moshe').draggable();
});

in the example i add to the body a div element after document ready event and then trying to make it draggable. 在示例中,我在文档准备事件后将div元素添加到主体,然后尝试使其可拖动。 the results are that it's not draggable, the classes are added to the element but it seems that it's not enough. 结果是它不可拖动,将类添加到了元素中,但这似乎还不够。

any ideas? 有任何想法吗?

update 更新

more info to clear things up.. i'm trying to move a readonly text input. 更多信息以解决问题..我正在尝试移动只读文本输入。

i created new jsfiddle at http://jsfiddle.net/70f3dbLh/2/ with the following javascript code: 我使用以下JavaScript代码在http://jsfiddle.net/70f3dbLh/2/创建了新的jsfiddle:

$(document).ready(function(){
    $("<div>").attr("id", "moshe").html("<input readonly type=\"text\"></input>").appendTo("body").draggable();
});

as you can see here.. the text input inside the div is not draggable 如您在这里看到的.. div中的文本输入不可拖动

i tried to create another example of trying to create a text input without a div and make it draggable.. same results. 我试图创建另一个示例,尝试创建不带div的文本输入并使它可拖动..相同的结果。 it's not draggable. 它是不可拖动的。

jsfiddle at http://jsfiddle.net/70f3dbLh/3/ jsfiddle位于http://jsfiddle.net/70f3dbLh/3/

javascript code: JavaScript代码:

$(document).ready(function(){
    $("<input readonly value=\"aaa\" type=\"text\"></input>").attr("id", "moshe").appendTo("body").draggable();
});

Use following jsfiddle: http://jsfiddle.net/70f3dbLh/1/ and look at this: http://jsfiddle.net/70f3dbLh/1/show/ 使用以下jsfiddle: http : //jsfiddle.net/70f3dbLh/1/并查看此内容: http : //jsfiddle.net/70f3dbLh/1/show/

The code is something like 代码类似于

$(document).ready(function(){
    $("<div>").attr("id", "moshe").html("hi").appendTo("body").draggable();
});

It is then draggable. 然后可以拖动。

Update on your update I updated my post, because you updated your post. 更新您的更新我更新了我的帖子,因为您更新了自己的帖子。

I added a new jsfiddle. 我添加了一个新的jsfiddle。 http://jsfiddle.net/8scyhq01/2/ In general, Input Fields are not draggable, but if you put a layer above the input field, this layer then is draggable. http://jsfiddle.net/8scyhq01/2/通常,“输入字段”不可拖动,但是如果在输入字段上方放置一层,则该层是可拖动的。 And the Input Field will be dragged too. 并且输入字段也将被拖动。

$("<div>").addClass("layer").attr("id", "moshe").html("<div class=\"layer\"></div><input readonly type=\"text\"></input>").appendTo("body").draggable();

I have modified a code I found, From this post to make text input draggable, putting a div on top of it. 我修改了我发现的代码, 在这篇文章中使文本输入可拖动,并在其顶部放置了一个div。

  $(function() { $( "#resizable" ).resizable({ containment: "#container" }); $( "#draggable" ).draggable({containment: "#container"}); }); 
  #container { width: 300px; height: 300px; } #container h3 { text-align: center; margin: 0; margin-bottom: 10px; } #resizable, #container { padding: 0.5em; } #d{ background:blue; width:100px; height:30px; position:relative; top: 2em; left:0em; z-index:9; opacity:0.1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <div id="container" class="ui-widget-content"> <h3 class="ui-widget-header">Containment</h3> <span id="draggable"><div id='d'></div> <input type="text "id="resizable" class="ui-state-active" readonly value="This is input box"> </span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM