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

javascript - does jquery have an equivalent of dojo.connect()? - Stack Overflow

programmeradmin0浏览0评论

Forgive my ignorance as I am not as familiar with jquery. Is there an equivalent to dojo.connect() ?

I found this solution : /

But there isn't disconnect feature !

Do you know other solution in jquery ? There are jquery.connect but this plugin not work in my tests.

Thanks for your help,
Stephane

Forgive my ignorance as I am not as familiar with jquery. Is there an equivalent to dojo.connect() ?

I found this solution : http://think-robot./2009/06/hitch-object-oriented-event-handlers-with-jquery/

But there isn't disconnect feature !

Do you know other solution in jquery ? There are jquery.connect but this plugin not work in my tests.

Thanks for your help,
Stephane

Share Improve this question edited May 25, 2023 at 16:10 aynber 23k9 gold badges54 silver badges67 bronze badges asked May 23, 2010 at 10:19 Stéphane KleinStéphane Klein 8841 gold badge11 silver badges21 bronze badges 1
  • Note that dojo.connect is being replaced by the dojo/on function: dojotoolkit/reference-guide/1.8/dojo/on.html – Steve Wranovsky Commented Oct 12, 2012 at 4:03
Add a ment  | 

4 Answers 4

Reset to default 3

The closest equivalent jQuery has is .bind(), for example:

$("#element").bind('eventName', function(e) {
 //stuff
});

And .unbind() to remove the handler, like this:

$("#element").unbind('eventName');

There are also shortcuts for .bind(), so for example click can be done 2 ways:

$("#element").bind('click', function() { alert('clicked!'); });
//or...
$("#element").click(function() { alert('clicked!'); });

There is also .live() (.die() to unbind) and .delegate() (.undelegate() to unbind) for event handlers based on bubbling rather than being directly attached, e.g. for dynamically created elements.

The examples above were anonymous functions, but you can provide a function directly just like dojo (or any javascript really), like this:

$("#element").click(myNamedFunction);

What do you mean by an equivalent to dojo.connect() ?

If you are willing to create your own custom event handlers, look at bind(), trigger() and proxy() in the Events.

The proxy() function will alow your to redefine what this points to in your callback function.

jQuery connect is equivalent to dojo.connect.

There isn't as such but you can acheive the same thing as follows:

$('#someid').click(function() { SomeFunc($(this)) });

That will wire somefunc, to the click event of control with id "someid". When the control is clicked the function will be called and the parameter will be a jquery object that refers to control that was clicked.

Update I have just done a bit more research and i think .delegate() is closer to what you want.

发布评论

评论列表(0)

  1. 暂无评论