我怎样才能制作我的锚标签(这个)

时间:2011-07-27 14:05:05

标签: javascript jquery

我遇到了一个小问题,我有一个充满锚标记的页面,当选择其中一个动画开始时,我现​​在唯一的问题是我没有将锚标记定义为(this)所以当选择了一系列锚点,每个锚点都执行动画,我现在不知道如何将a标签更改为此?

到目前为止我的代码是:

$('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();
        });

2 个答案:

答案 0 :(得分:2)

您想要像您的

一样为您的锚标签添加ID
<a id="myTag" href=""></a>

然后您可以像这样访问它

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

每页只能使用一次id(意味着每个id都是唯一的,而不是每页只能有1个id。)

以下是来自Jquery的id-selector以及更多selectors的更多信息

答案 1 :(得分:1)

您不使用var个关键字。这很糟糕,因为您以这种方式创建的所有变量都在全局范围内相互覆盖

$('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();
});
相关问题