开发者

JQuery: Bind click on hrefs

开发者 https://www.devze.com 2023-02-21 02:17 出处:网络
I\'m trying to replace my hrefs links by JQuery click events: for that I 开发者_StackOverflow中文版have placed a insite class on everyconcerned.

I'm trying to replace my hrefs links by JQuery click events: for that I 开发者_StackOverflow中文版have placed a insite class on every concerned.

This is the code I have placed at the bottom of my page:

$('.insite').each(function(i,a) {
    $('this').click(function() {
        $('#content').load(a.attr('href')+' #content');
    });
});

Did I forget anything?

Thanks


$('.insite').click(function(ev) {
    $('#content').load($(this).attr('href')+' #content');
    ev.preventDefault();
});

You don't need the each. And the ev.preventDefault is needed so it doesn't load the href in the browser.


Did you include the jQuery library? Also, is your code wrapped in a DOM-ready event?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>

$(function() {
   $('.insite').click(function(e) {
     $('#content').load($(this).attr('href')+' #content');
   });
});


$('.insite').click(function(){
    $('#content').load($(this).attr('href') + ' #content');
});


You don't need the each. You can just do:

$('.insite').click(function(e) {
    e.preventDefault();
    // Do stuff
});

No idea what you are trying to do with $('#content').load(a.attr('href')+' #content');


You should use

$(this)

instead of

$('this')
0

精彩评论

暂无评论...
验证码 换一张
取 消