简体   繁体   English

jQuery拖放停止在某些对象上工作?

[英]JQuery drag and drop stops working on certain objects?

I've successfully been able to make elements (like div's, panel's, whatever) draggable by using this bit of Jquery: 通过使用以下Jquery,我已经成功地使元素(如div,面板等)可拖动:

<script type="text/javascript">
      $(function() {
          $(".drag").draggable({ revert: true });
      });
</script>

This works fine for anything that already exists on the page before page_load is done and before any postbacks occur. 对于在page_load完成之前和发生任何回发之前页面上已经存在的任何内容,此方法都可以正常工作。 I have a series of buttons (with a class of '.catbuttons') that when clicked, will call out to my database and retrieve some image url's. 我有一系列按钮(带有“ .catbuttons”类),当单击这些按钮时,它们将调出到我的数据库并检索一些图像URL。 Once I retrieve this set of url's, I do a for/next loop and create a whole bunch of image objects and place them into a panel. 检索到这组网址后,我将进行for / next循环并创建一堆图像对象并将其放置在面板中。 Each of the images has the cssclass of '.drag' to make it work with the JQuery above. 每个图像都具有css类“ .drag”,以使其与上面的JQuery一起使用。

Problem is, they no longer drag! 问题是,它们不再拖动! I've read another post on here about needing to rebind javascript stuff after a postback (even a partial postback?) and this would allow brand new controls to get the above Jquery code attached to them, and hence make them draggable. 我在这里读了另一篇文章,内容涉及需要在回发(甚至是部分回发)之后重新绑定javascript东西,这将允许全新的控件将上面的Jquery代码附加到它们上,从而使它们可拖动。 After some google searching, I ran into this and tested it out: 经过一些谷歌搜索,我遇到了这个并测试了它:

<script type="text/javascript">
        $(".catbuttons").live("click", function(){
            $(".drag").draggable({ revert: true });
        });
 </script>

I tried leaving this in the header section of my aspx page with the original script, and also having this here all by itself. 我尝试使用原始脚本将其保留在我的aspx页面的页眉部分中,并且也将其全部保留在此处。 No dice either way. 两种方法都没有骰子。

Thoughts? 有什么想法吗?

The above function actually expect a click from user, to call draggable again. 上面的功能实际上期望用户点击,以再次调用可拖动对象。 Instead you need to do it after postback. 相反,您需要在回发后执行此操作。 Rule here, should be, to register all images with draggable function, after placing the images on page. 将图像放置在页面上之后,此处的规则应为使用可拖动功能注册所有图像。

Edit: In your case clicking button just triggers the function to fetch the images. 编辑:在您的情况下,单击按钮仅触发获取图像的功能。 Those images should be placed on page, before you make them draggables. 在将这些图像设为可拖动对象之前,应将其放置在页面上。 You can register the created object with draggables, immediately after the statements where you place & apply ".drag" cssclass to those object/images at runtime. 您可以在运行时将“ .drag” cssclass放置并应用到这些对象/图像的语句之后,立即使用可拖动对象注册创建的对象。

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

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