Jquery - 从 getJSON 添加动态添加的控件
我在将控件分配给动态添加的元素(在 JSON 函数内)时遇到问题。
代码工作正常,但是当我添加 $('div.prnt').append("hello"); 时在 JSON 内部,它不会分配 $("div.text").draggable();到它。当我拿走 JSON 函数时,我只使用 $('div.prnt').append("hello");它立即起作用。
我想象 JSON 函数会在调用可拖动控件之前阻止添加元素。
<style>
div.prnt div.test
{
border: 1px solid #000;
width: 200px;
height: 200px;
}
</style>
<script type="text/javascript">
$(document).ready(function ()
{
var serviceurl = "WebServices/BusinessCardStudio.svc/gettextobjects/3457/17/17/1064/709/600/399";
$.getJSON
(
serviceurl,
function (data)
{
$.each(data, function (i, item)
{
$('div.prnt').append("<div class='object text'>hello</div>");
});
}
);
$("div.text").draggable();
});
</script>
<div class="prnt">
</div>
有谁知道解决这个问题的方法吗?
提前致谢
I am having a problem assigning a control to dynamically added elements (inside the JSON function).
The code is working fine but when I add $('div.prnt').append("hello"); inside the JSON it then doesn't assign the $("div.text").draggable(); to it. When I take the JSON function away and i'm using just $('div.prnt').append("hello"); it works straight away.
I imagine the JSON function is stopping the elements from being added before the draggable control is called.
<style>
div.prnt div.test
{
border: 1px solid #000;
width: 200px;
height: 200px;
}
</style>
<script type="text/javascript">
$(document).ready(function ()
{
var serviceurl = "WebServices/BusinessCardStudio.svc/gettextobjects/3457/17/17/1064/709/600/399";
$.getJSON
(
serviceurl,
function (data)
{
$.each(data, function (i, item)
{
$('div.prnt').append("<div class='object text'>hello</div>");
});
}
);
$("div.text").draggable();
});
</script>
<div class="prnt">
</div>
Does anyone know a way to get around this?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$("div.text").draggable();
在 JSON 请求开始后立即调用。然后,服务器用 JSON 进行响应并执行您传递给它的函数。所以你正在制作一些尚不存在的可拖动的东西。将可拖动的调用放在回调函数中,应该就可以了。
$("div.text").draggable();
is called immedately after your JSON request starts. Then later, the server responds with JSON and executes the function you pass to it. So you are making something draggable that doesnt exist yet.Put the draggable call in the callback function and you should be good.
在ajax成功时执行此操作,这样就保证ajax完成后其done
do it in the success of ajax, so that it will ensure its done after the ajax is completed
AJAX 是异步。
getJSON()
调用将立即返回,并且您的draggable()
调用将在服务器回复之前运行。您需要将
draggable()
调用移至回调中。这就是回调存在的原因。
AJAX is asynchronous.
The
getJSON()
call will return immediately, and yourdraggable()
call will run before the server replies.You need to move the
draggable()
call into the callback.This is why the callback exists in the first place.