简体   繁体   English

jQuery无法识别点击

[英]Jquery isn't recognizing click

I am building a webapp (I'm new to javascript), and jquery refuses to do anything when I click on something that has a .click listener attached to it. 我正在构建一个webapp(我是javascript新手),当我单击带有.click侦听器的东西时,jQuery拒绝执行任何操作。 In addition, it won't animate. 此外,它不会动画。 I am doing something wrong, and can't figure what. 我做错了,无法弄清楚。

Code: 码:

 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: (don't mind the horrible graphics, I didn't want to upload my actual ones as they don't yet have a copyright. http://jsfiddle.net/hFBMB/ ) JSfiddle:(不要介意可怕的图形,我不想上传我的实际图形,因为它们还没有版权。http: //jsfiddle.net/hFBMB/

You will have to call loadTabBar() or instead of that you can put your code in ready instead of loadTabBar(). 您将必须调用loadTabBar(),否则,您可以准备好代码而不是loadTabBar()。

$(document).ready(function(){

//code goes here

});

You aren't ever calling loadTabBar() . 您从未调用过loadTabBar() I'm not sure why you have everything wrapped inside it? 我不确定为什么要将所有东西都包裹在里面?

You have to call your loadTabBar() function before you can use any of the stuff inside. 您必须先调用loadTabBar()函数,然后才能使用其中的任何东西。 Try calling your function, then the clicks should work... 尝试调用您的函数,然后单击应该起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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