-2

jQuery: get element from server onclick work

$(function() {
   $('.class1 a, .class2 a').click(function(e) {    
      href = $(this).attr("href");              
      $.post(href,function(data) { 
         $('#content').html('');        
         $('#content').append(data);          
      });   
   });
});

if class2 back form server such as ..,that onclick function doesn't work on this class,how to fix this?

2 Answers 2

1

You need preventDefault:

$('.class1 a, .class2 a').click(function(e) {    
  e.preventDefault();
  ...
});
Sign up to request clarification or add additional context in comments.

8 Comments

e.preventDefault(); this not the problem
@miket Your ajax request can't work without preventDefault, because as I can see your link is not empty and it's not a hash
The full code contains e.preventDefault();,above is a fragment
If you click a tag within class1,then in container there's a a tag within class2 back from sever ,but this class2 a tag onclick doesnt work
@miket As Stefan write you need api.jquery.com/live or api.jquery.com/on on container of this elements, which one to use depends on your jquery version.
|
1

Try using 'on' with a filter on 'class1 a, class2 a' on the body elemnt instead of searching the page for all classes.

Explanation: $('.class1 a, .class2 a'). searches for all elements an adds a event. When you add content later on, the event isn't added to it.

3 Comments

if class1 a and class2 are not back from sever ,even up to class8 it's works ok.
The problme is how to get the element from server onclick work
Stefan is right ,by the clue Stefan give I found this : stackoverflow.com/questions/815856/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.