Jquery没有识别出点击

时间:2013-08-23 18:51:54

标签: javascript jquery html css

我正在构建一个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/

3 个答案:

答案 0 :(得分:2)

您必须调用loadTabBar(),否则您可以将代码放入ready而不是loadTabBar()。

$(document).ready(function(){

//code goes here

});

答案 1 :(得分:1)

你永远不会打电话给loadTabBar()。我不确定你为什么把所有东西包裹在里面?

答案 2 :(得分:1)

您必须先调用loadTabBar()函数,然后才能使用其中的任何内容。尝试调用您的函数,然后点击应该有效...