jQuery 可拖动的问题
我无法让下面循环中的最后一个可拖动项目变为可拖动(最后一个项目之前的所有其他项目都很好,它们都是可拖动的)。当我查看源代码时,它输出到我的浏览器的最后一个可拖动项目的 html 看起来很好,除了最后一个可拖动项目之外,“ui-draggable”类没有附加到 div (当使用 firebug 进行查看时)。有什么想法吗?谢谢
<?php foreach ($categorySliderImages->result() as $idx => $row): ?>
<li>
<div class="slider">
<div class="slide">
<h2><?= $row->Header ?></h2>
<div><?= $row->Teaser ?></div>
<p><a href="/products/product/<?= $row->PID; ?>/<?= $row->FID; ?>">Click Here</a> for more information</p>
</div>
<?php $subImages = $this->products_model->get_category_slider_images($row->PID); ?>
<?php foreach ($subImages->result() as $idx2 => $row): ?>
<div id="catdraggable<?= $row->IID ?>" style="position: absolute; top:<?= $row->SliderTop ?>px; left:<?= $row->SliderLeft ?>px;">
<img src="/FLPRODIMGS/thumb/<?= $row->Name ?>" />
</div>
<script type="text/javascript">
$(function () {
$("#catdraggable<?= $row->IID ?>").draggable({
stop: function (event, ui) {
p = $("#catdraggable<?= $row->IID ?>").position();
$.post("/admin/set_slider_image_position", {
id: '<?= $row->IID ?>',
top: p.top,
left: p.left,
context: 'cat'
})
}
});
});
</script>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</li>
I'm having trouble getting the very last draggable item in the loop below to become draggable (all the other items before the last item are fine, they are draggable). The html it's outputting to my browser for the last draggable item looks fine when I view source exept for the very last draggable, the "ui-draggable" class isnt being attached to the div (when looking with firebug). Any ideas? Thanks
<?php foreach ($categorySliderImages->result() as $idx => $row): ?>
<li>
<div class="slider">
<div class="slide">
<h2><?= $row->Header ?></h2>
<div><?= $row->Teaser ?></div>
<p><a href="/products/product/<?= $row->PID; ?>/<?= $row->FID; ?>">Click Here</a> for more information</p>
</div>
<?php $subImages = $this->products_model->get_category_slider_images($row->PID); ?>
<?php foreach ($subImages->result() as $idx2 => $row): ?>
<div id="catdraggable<?= $row->IID ?>" style="position: absolute; top:<?= $row->SliderTop ?>px; left:<?= $row->SliderLeft ?>px;">
<img src="/FLPRODIMGS/thumb/<?= $row->Name ?>" />
</div>
<script type="text/javascript">
$(function () {
$("#catdraggable<?= $row->IID ?>").draggable({
stop: function (event, ui) {
p = $("#catdraggable<?= $row->IID ?>").position();
$.post("/admin/set_slider_image_position", {
id: '<?= $row->IID ?>',
top: p.top,
left: p.left,
context: 'cat'
})
}
});
});
</script>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</li>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否会解决您的问题,但至少它会稍微减少页面的大小:不要为每个循环迭代重复脚本,而是将其移到 foreach 之外,并添加一个类您想要使其可拖动的 div。
类似于:
data-yourid
属性,以便您稍后可以读取它,并且它是有效的 HTML5。最后,以下脚本(仅一次,即在
endforeach
之后):I don't know if this will fix your problem, but at the very least it will reduce the size of your page a little: instead of the script being repeated for each loop iteration, move it outside the foreachs, and add a class to the divs that you want to make draggable.
So something like:
The
data-yourid
attribute is so you can read it later, and it's valid HTML5.And at the end, the following script (only one time, i.e. after the
endforeach
s):尝试将目标可拖动元素包含在包含 div 中,并让 jQuery 引用它。这使您的声明更加清晰:
然后您可以在 jQuery 声明中得到以下内容:
Try enclosing your target draggable elements in a containing div and have jQuery reference that. This makes your declaration a little cleaner:
You then get this for your jQuery statement: