简体   繁体   中英

How can I make my anchor tag (this)

I've run into a small problem, Ive got a page full of anchor tags and when 1 of them is selected an animation begins, my only problem right now is that I havent defined the anchor tag as (this) so when a series of anchors are selected each of them perform the animation, im not sur right now how I can change the a tag to this though?

My code so far is:

$('a').bind('click', function(e){
            $ajax = $('<div id="ajax"></div>');
            $ajax.prependTo('#container');
            $('html, body').animate({ scrollTop: 0 }, 'fast', function(){
                $ajax.animate({ height: 300 }, 'slow', function(){ 
                    $preloader = $('<div id="preloader"></div>').hide();
                    $preloader.prependTo('#ajax').fadeIn('normal');
                });
            });
            e.preventDefault();
        });

you want to add an id to your anchor tag like this

<a id="myTag" href=""></a>

Then you can access it like this

$('#myTag').bind(...

You should only use an id once per page (meaning that every id is unique, not that there can only be 1 id per page).

Here's some more info on the id-selector from Jquery and more selectors in general

You do not use var keywords. This is bad, as all variables you create this way are in the global scope overwriting each other

$('a').bind('click', function(e){
    var $ajax = $('<div></div>').prependTo('#container');

    $('html, body').animate({ scrollTop: 0 }, 'fast', function(){
        $ajax.animate({ height: 300 }, 'slow', function(){ 
            $('<div></div>').hide().prependTo($ajax).fadeIn('normal');
            // you can refer to $ajax here! --^^^^^
        });
    });
    e.preventDefault();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM