How to get the child of an element being dragged w

2019-03-05 23:48发布

问题:

I have the following html:

<div id="gallery">
<ul>
<li>
<a href="url I want to get">link</a>
</li>
</ul>
</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 } );
                            }


            });

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.

回答1:

You can use:

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

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')


回答2:

Try this:

$(this).children('a').attr("href");

Does that work for you?