0

我正在构建一个 webapp(我是 javascript 新手),当我单击附加了 .click 侦听器的东西时,jquery 拒绝执行任何操作。此外,它不会动画。我做错了什么,不知道是什么。

代码:

 function loadTabBar()
{
    person = false;
    sale = false;
    current = false;
    wine = false;
    if(!person && !sale && !current && !wine)
    {
        justOpened();
    }

    function useTabBar(){

    $('#PersonDiv').click(function()
    {
        alert('hi')
        activatePerson();
    });
    $('#Current').click(function()
            {
                activateCurrent();
            });
    $('#Sale').click(function()
            {
                activateSale();
            });
    $('#Wine').click(function()
            {
                activateWine();
            });

    function activatePerson()
    {
        if(!person)
        {
            var newImg="#Person";
            if(sale)
            {
                var oldImg="#Sale"
                changeImg(oldImg, newImg);
            }
            if(wine)
            {
                var oldImg="#Sale"
                changeImg(oldImg, newImg);
            }
            if(current)
            {
                var oldImg="#Sale"
                changeImg(oldImg, newImg);
            }
            person = true;
            current = false;
            wine = false;
            sale = false;

        }
    }
    function activateSale()
    {
        if(!sale)
        {
            var newImg="#Sale"
            if(person)
            {

                var oldImg="#Person"
                changeImg(oldImg, newImg);
            }
            if(wine)
            {
                var oldImg="#Wine"
                changeImg(oldImg, newImg);
            }
            if(current)
            {
                var oldImg="#Current"
                changeImg(oldImg, newImg);
            }
            person = false;
            current = false;
            wine = false;
            sale = true;
        }
    }
    function activateWine()
    {
        if(!wine)
        {
            var NewImg = "#Wine"
            if(sale)
            {
                var oldImg="#Sale"
                changeImg(oldImg, newImg);
            }
            if(person)
            {
                var oldImg="#Person"
                changeImg(oldImg, newImg);
            }
            if(current)
            {
                var oldImg="#Current"
                changeImg(oldImg, newImg);
            }
            person = false;
            current = false;
            wine = true;
            sale = false;
        }
    }
    function activateCurrent()
    {
        var newImg = "#Current";
        if(!current)
        {
            if(sale)
            {
                var oldImg="#Sale"
                changeImg(oldImg, newImg);
            }
            if(wine)
            {
                var oldImg="#Wine"
                changeImg(oldImg, newImg);
            }
            if(person)
            {
                var oldImg="#Person"
                changeImg(oldImg, newImg);
            }
            person = false;
            current = true;
            wine = false;
            sale = false;
        }
    }
    function changeImg(oldImg, newImg)
    {
            $(oldImg).fadeOut('fast', function()
            {
                $(this).attr('src', ('http://www.jagspcmagic.com/' + oldImg.substring(1) + '1.png'), function(){
                    if(this.complete) $(this.fadeIn('fast'));
                });
            })                              
            $(newImg).fadeOut('fast', function()
            {
                $(this).attr('src', ('http://www.jagspcmagic.com/' + oldImg.substring(1) + '2.png'), function(){
                    if(this.complete) $(this.fadeIn('fast'));
                });
            })

    }
}function justOpened()
{
    $('#Person').fadeOut('fast', function()
            {
                $('#Person').attr('src', 'http://www.jagspcmagic.com/Person2.png', function(){
                    $(this.fadeIn('fast'));
                });
            })  
    person = true;
    useTabBar();
}}

JSfiddle:(不要介意可怕的图形,我不想上传我的实际图形,因为它们还没有版权。http: //jsfiddle.net/hFBMB/

4

3 回答 3

2

您将不得不调用 loadTabBar(),或者您可以将代码准备好而不是 loadTabBar()。

$(document).ready(function(){

//code goes here

});
于 2013-08-23T18:55:40.207 回答
1

你从来没有打电话loadTabBar()。我不知道你为什么把所有东西都包在里面?

于 2013-08-23T18:55:09.033 回答
1

你必须先调用你的loadTabBar()函数,然后才能使用里面的任何东西。尝试调用你的函数,然后点击应该工作......

于 2013-08-23T18:55:26.250 回答