Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

javascript - Custom helper for jQuery UI Draggable

I have a jQuery UI draggable, and I've tried to create a custom helper which would contain some but not all information of the original element.

Here's my draggable elements;

<ul>
  <li><div>Name</div> <span>12-12-2011</span></li>
  <li><div>Another name</div> <span>12-12-2011</span></li>
</ul>

And jQuery;

$("ul li").draggable(function(){
  helper: function(){
    return $("<div></div>");
  }
});

The idea would be that while dragging, the user would have a helper that contains only the name, but not the time element.

My actual code is slightly more complex than this even, so what I'm really looking for is any selector that would allow me to select the original element, or a copy of it, and then do all kinds of jQuery magic on it (filtering elements, adding new elements, classes, etc.)

However, for my life I can't find this, the documentation of draggable sucks and nobody at #jquery would help me. Any ideas?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First your way of calling draggable is faulty. The expected parameter is an object literal, not a function.

this is the currently dragged element in the helper function.

Having the following html

<ul>
  <li><div class="name">Name</div> <span>12-12-2011</span></li>
  <li><div class="name">Another name</div> <span>12-12-2011</span></li>
</ul>

You can do this:

$('ul li').draggable({
    helper: function() {
        //debugger;
        return $("<div></div>").append($(this).find('.name').clone());
    }
});

Note: I have added class to the <div> representing the name for the sake of selecting it but you can find any other way to do so.

Here's a jsfiddle for you to check.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...