最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery - Toggle mousedown event without destroying it in the dom? - Stack Overflow

programmeradmin5浏览0评论

So on Friday I asked this question and it wasn't well written, let me explain this in better detail:

So this might sound remedial, but I have this container that I include a mousedown event and all I want to do is toggle it without destroying the properties of it.

If it do :

 $("#div").unbind("mousedown")  // This will just destroy the event.

I was thinking I could move the event to a dom element that isn't being used? And then just switch it back when I'm done...

So this is whats happening : I have a plugin lets just call it Vinny for now

  $("#div").vinny(settings);

vinny has a mousedown event that I want to enable/disable via a trigger.

I was thinking I would have a $fn.disableMouseDown function that could disable it, but was curious if theirs a way to unbind a mouseDown on the dom but not destroy it?

If you know of a quick way of doing it help me out! Thanks, Vinny

So on Friday I asked this question and it wasn't well written, let me explain this in better detail:

So this might sound remedial, but I have this container that I include a mousedown event and all I want to do is toggle it without destroying the properties of it.

If it do :

 $("#div").unbind("mousedown")  // This will just destroy the event.

I was thinking I could move the event to a dom element that isn't being used? And then just switch it back when I'm done...

So this is whats happening : I have a plugin lets just call it Vinny for now

  $("#div").vinny(settings);

vinny has a mousedown event that I want to enable/disable via a trigger.

I was thinking I would have a $fn.disableMouseDown function that could disable it, but was curious if theirs a way to unbind a mouseDown on the dom but not destroy it?

If you know of a quick way of doing it help me out! Thanks, Vinny

Share Improve this question edited May 1, 2011 at 19:14 Vinny asked Apr 29, 2011 at 20:41 VinnyVinny 1331 gold badge3 silver badges10 bronze badges 3
  • Forgot to mention! That the bind event is wrappend in an object So I have $("#div").plugin() inside of plugin i have the mousedown event – Vinny Commented Apr 29, 2011 at 21:06
  • 2 I think we will need more details if you need our help :) – morgar Commented Apr 29, 2011 at 21:11
  • What are you trying to toggle by invoking a mouse down? Is this being done someplace else in the DOM? You could just tell it to run the same function or something along those lines? – afuzzyllama Commented Apr 29, 2011 at 21:39
Add a ment  | 

5 Answers 5

Reset to default 4

Put your mand inside a function, so you can bind/unbind with one line only, i.e:

function some() {
    // some mands
}

$("#div").bind("mousedown", some);
$("#div").unbind("mousedown");

One approach is to just use a named function, bind it when it's needed and unbind it when it's not:

function foo () { 
    // do something on mousedown
}

// When needed:
$("#div").bind("mousedown", foo);

// When not needed:
$("#div").unbind("mousedown", foo);

I would just stick an if(toggle) statement inside the event. Any reason you can't do that? (only thing i can think of is you wouldn't want to have the event being continually fired over and over, which makes sense - is that the case?)

here is a working example http://jsfiddle/samccone/ENzyk/

I think this is a simple and elegant solution

Hey guys thanks for all the ideas, but I kinda did a hacky way of approaching this.

I explainz:

So this plugin on the mousedown is binded in the plugin.init() and in their i defined a local function checks disableValue and in their I just check the dom for a or do a bool return and run that against the other function that was already present in exiting the mousedown event.

Make sense? I hope so too

Thanks,

发布评论

评论列表(0)

  1. 暂无评论