如何使用 jQuery UI 获取被拖动元素的子元素 [英] How to get the child of an element being dragged with jQuery UI

查看:30
本文介绍了如何使用 jQuery UI 获取被拖动元素的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下html:

<div id="gallery">
<ul>
<li>
<a href="url I want to get">link</a>
</li>
</ul>
</div>

还有一些 jQuery 允许它被放到另一个 div 上:

and some jQuery that allows it to be dropped on another div:

$trash.droppable({
                accept: '#gallery > li',
                activeClass: 'ui-state-highlight',
                drop: function(ev, ui) {
                    deleteImage(ui.draggable);

          var  $flickrparenturl =  $("a").attr("href");  //only gets href of <li> #1, not <li> being dragged

             $.post("updateDB.php", { 'flickrparenturl': $flickrparenturl } );
                            }


            });

获取被拖动元素的子元素的 href 属性的正确方法是什么?$("a").attr("href");只获取页面上第一个 li 的 href,而不是被拖动的那个.

What is the correct way to get the href attribute of the child of the element being dragged? $("a").attr("href"); is only getting the href of the 1st li on the page, not the one being dragged.

推荐答案

你可以使用:

ui.draggable.find('a').attr('href')

这将在可拖动元素的所有后代中查找 a 元素.

This will look for the a element among all descendants of the draggable element.

要仅查看可拖动对象的直接子级,请改用:

To look only at the direct children of the draggable, use this instead:

ui.draggable.children('a').attr('href')

这篇关于如何使用 jQuery UI 获取被拖动元素的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆