简体   繁体   中英

jQuery IE6 click problem

Is there any reason why the click doesn't work in IE6 on the following JQuery code? See ...$('#toggleVAT').click(function... It works in IE7 and FF?

function switchButton(to){
    if(to === 'INC'){
    $('#toggleVAT').removeClass("exc");
    $('#toggleVAT').addClass("inc");
    $('#toggleVAT em').text("inc.");
    } else {
    $('#toggleVAT').addClass("exc");
    $('#toggleVAT').removeClass("inc");
    $('#toggleVAT em').text("exc.");
    }
}

function switchPrices(){
    if($.cookie('VATMODE') == "INC"){
    $('.price .incVAT').show();
    $('.price .excVAT').hide();
    switchButton('INC');
    } else {
    $('.price .incVAT').hide();
    $('.price .excVAT').show();
    switchButton('EX');
    }
}


$(function(){
    switchPrices();
    $('#toggleVAT').click(function(){
    if($.cookie('VATMODE') === 'INC'){
        $.cookie('VATMODE', 'EX');
        switchButton('EX');
    } else {
        $.cookie('VATMODE', 'INC');
        switchButton('INC');
    }
    switchPrices();
    return false;
    });
});

On IE6 switchPrices() runs once, but it does not execute the code when I click #toggleVAT. I am using latest minified jQuery. #toggleVAT is just a paragraph. I am using IETester http://my-debugbar.com/wiki/IETester/HomePage . I checked it on natively running IE6 before and the bahaviour was the same. I've also rulled out possible CSS issues as the problem persists without stylesheet.

Is toggleVAT a link? Try returning false in the callback.

$('#toggleVAT').click(function(){
        if($.cookie('VATMODE') === 'INC'){
            $.cookie('VATMODE', 'EX');
            switchButton('EX');
        } else {
            $.cookie('VATMODE', 'INC');
            switchButton('INC');
        }
        switchPrices();
        return false;
    });

Doing this you prevent the default click behavior being executed.

I recall that there are problems when you have multiple (--> hacked) IE versions running on the same windows.

Also if you use the IE Developer Toolbar check that it doesn't disable cookies.


For me your code works without problems in IE6, FF 3.0.11 and Opera 9.64.

I provided a small sample. To test out if you still can reproduce the error.

http://pastebin.com/m7e8d87c6

jquery.js --> http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js

jquery.cookie.js --> http://plugins.jquery.com/files/jquery.cookie.js.txt

Aren't you calling switchButton code twice? Once inside the click handler and second time inside the switchPrices function? Wouldn't that toggle the values back to the original state?

Try this:

Aren't you calling switchButtonc ode twice? Once inside the click handler and second time inside the switchPrices function?

Try this:

$(
    function()
    {
        $('#toggleVAT').click(
            function()
            {
                switchPrices();
                return false;
            }
           );
        switchPrices();
    }
);

function switchPrices()
{
    var targetVatMode = $.cookie('VATMODE') == 'INC' ? 'EX' : 'INC';
    $.cookie('VATMODE', targetVatMode);
    var removeClassName = targetVatMode == 'INC' ? 'exc' : 'inc';
    var addClassName = targetVatMode == 'INC' ? 'inc' : 'exc';
    var displayText = targetVatMode == 'INC' ? 'inc.' : 'exc.';

    var elementToShow = targetVatMode == 'INC' ? '.price .incVAT' : '.price .excVAT';
    var elementToHide = targetVatMode == 'INC' ? '.price .excVAT' : '.price .incVAT';

    $(elementToShow).show();
    $(elementToHide).hide();


    $('#toggleVAT')
        .addClass(addClassName)
        .removeClass(removeClassName)
        .find('em')
            .text(displayText);
}

Also, if you are okie with Vat Mode as 'EXC' instead of 'EX', you can simplify the code even further.

function switchPrices()
{
    var oldVatMode = $.cookie('VATMODE');
     //Define your Vat mode as EXC instead of EX.
    var newVatMode = oldVatMode == 'INC' ? 'EXC' : 'INC';
    $.cookie('VATMODE', newVatMode);

$('#toggleVAT')
        .addClass(newVatMode.toLowerCase()) 
        .removeClass(oldVatMode.toLowerCase())
        .find('em')
            .text(newVatMode.toLowerCase() + '.');

    $('.price .' + newVatMode + 'VAT').show();
    $('.price .' + oldVatMode + 'VAT').hide();
}

Just a small thing, IETester cannot read cookies . This might be affecting things. Although, you said you tested in a native IE6, so it's probably not the issue, but it's worth knowing about.

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