English 中文(简体)
Fictitious jQuery persist method
原标题:

Is there a way to have a declaration such as the following persist to all matching elements that are later added to the DOM?

$("a.my-class").replaceWith("<span>Replaced</span>");

Something along the lines of...

$("a.my-class").persist().replaceWith("<span>Replaced</span>");

(Persist is a fictitious method, that I hope conveys what I would like to accomplish.)

问题回答

There isn t a method exactly like what you want, but if the content is being added using the jQuery AJAX methods, you can use this:

$("<div></div>").ajaxSuccess(function(){
   $("a.my-class").replaceWith("<span>Replaced</span>");
});

And this code will run after every successful AJAX request, provided the requests are made using a jQuery $.ajax call (including $.post or $.get). You only need to call this once on your page and it will trigger on any AJAX call made.

If you run into trouble with the replacement happening too soon:

$("<div></div>").ajaxSuccess(function(){
   window.setTimeout( function(){
       $("a.my-class").replaceWith("<span>Replaced</span>");
   }, 250);
});

The live() method in jquery does something similar but it is for events.

You could possibly use live() and the load event to achieve what you are looking for, something like (untested):

$("a.my-class").live( load ,function(){
  $(this).replaceWith("<span>Replaced</span>");
});




相关问题
Python: Binding method

In following example I am trying to bind a method object via types.MethodType(...). It does not seem to work. Any suggestions? Thanks in advance. import types class Base: def payload(self, *args)...

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Behaviour of final static method

I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance....

Setting the Page-Title in .Net using C# from a Class

Question, How do i set the title of a page from a class. Is it even possible? I can and have set the page title from a page itself and a usercontrol. Can I, How Do I do this via a class using C# ....

how to drag in the animation in iphone application?

Iphone application is using the static co-ordinates to reach at some points in the application,which is based on the button event. But what i want is when i drag the item than it should reach at some ...

热门标签