Sunday 15 May 2011

javascript - How to use jQuery to check if an element is added to page? -


So I am using, and when I scrolled X pixels, add new objects to the page Will go

(I want to check how I have been added to a new element ( class or id ) or not because because if a new Element is added then I want to do some jQuery on the new element.)

just use it. Length, to check that it exists, after an append ()

  $ ('# elemId'). Length;   

It is ...

  if ($ ('# elemId'). Length> 0) {// Your code here} ;   

Or type your own function ....

  jQuery.fn.Exists = function () {return jQuery (this). Length> 0; };   

Then use it to return true or false ...

  if ($ ('# elemId'). Exists ()) { // your code here};   

Or you can use the livequery () plugin ...

  $ ('# elemID'). Livequery (function () {// do things binding here are new events and stuff like this // This function is called when an object is added. // Check the function of the delete function and similarly. });   

I can not find more plugin on the jQuery website, but it was present at one point

You can also try domaineinded ... < / P>

  $ (document) .bind ('Dominied', function (e) {console.log (e.target, 'was inserted');});   

You can use it on () .... If your selector is always the same ...

For example, as you scroll , You say always adding a new div named class = "new device" ....

Then you want to do the jackpot .... if you force to click on an event Want to ... $ (document) .on ('click', '.newdiv', function () {// your code here});

No comments:

Post a Comment