1

Well, I inserted an anchor tag with jquery:

 $.post(url, {key:key}, function(data) { 
    $('#links').html(data); 
    }); 

data returned from a php file is:

<a href="#" class="some-class">Hypertext<a>

When this link is clicked i want to return false so i did:

 <a href="#" class="some-class" onclick="someFunction(); return false">Hypertext</a>

but it isn't returning false, but the function is called. I tried to return false from the function it isn't working either.

And when I tried to use:

$('.some-class').click(function() {
   // something
   }); 

this didn't call the function. I couldn't find a way to fix this, any help?

4

3 回答 3

0

You need to use event delegation using jQuery.on

$('#links').on('click', '.some-class', function(){
})
于 2013-01-30T09:11:23.103 回答
0

You need to use on() with a delegated event handler as the .some-class element is being dynamically appended. You'll also want to use event.preventDefault() to stop the standard link behaviour. Try this:

$('#links').on('click', '.some-class', function(e) {
    e.preventDefault(); // stop link behaviour
    // do something...
})
于 2013-01-30T09:13:22.943 回答
0

Try to bind click when content are loaded.

于 2013-01-30T09:14:40.387 回答