views:

1037

answers:

2
...

There are listeners on $('#target') and its children.

When we call $('#target').remove() to delete it from DOM,will the listeners be removed?

If not,how to remove them together?

+4  A: 

Depends on how you add the listeners. If you do

$("#target").click(function() { doSomething(); });

Then remove() will also delete the listener. However, if you do:

$("#target").live("click", function() { doSomething(); });

The listener will not be removed. What you've done here is to create a live HTML collection, and if you at a later point add a new element with id of "target", it will get this listenere added to it.

peirix
In my application,some listeners are added by html files loaded by $.load(),will they be removed?
Shore
Yes.
peirix
+1  A: 

Yes, the directly bound event handlers and the internally cached data will be removed.

From Manipulation/remove:

"Note that this function starting with 1.2.2 will also remove all event handlers and internally cached data."

CMS
What about the listeners added by html file loaded by $.load()?
Shore