奇怪的IE JQuery问题

时间:2009-09-16 02:37:36

标签: jquery internet-explorer

适用于chrome,适用于ff,但不适用于IE。

IE拒绝在我的回调中调用该函数。

我会发布我认为相关的代码:

function slideRight(){
    disableAll();

    var leftMarg = parseFloat(opiHolder.css("marginLeft"));
    var fullWidth = parseFloat(opiHolder.css("width"));

    if(fullWidth + leftMarg > opiWidth) {

        opiHolder.animate({marginLeft: '-=' + (fullWidth + leftMarg - opiWidth) + 'px'}, 'slow', function() {

            alert("this is working now");       

            enableAll();

        });

    } else {
        enableAll();
    }       
}

这看起来很奇怪所以我做了一个实验:

$(document).ready(function() {
    var a = "1000";
    var b = "800";

    var c = parseFloat(a);
    var d = parseFloat(b);

    $("#suck").animate({marginLeft: '-=' + (c + d) + 'px'}, 'slow', function(){funcX();});              

    function funcX(){
        alert("here");
    }
});

这完美无缺。

我可以看到“此处”警报,但不会显示“此处正在运行”警报。它真的让我失望了!

我尽可能地复制条件......

发生了什么事?

任何帮助将不胜感激。

谢谢!

- 麦克

编辑:这是代码......

$(document).ready(function() {
    var viewer = $("#viewer");
    var lock = false;

    var contactsCounter = 1;
    var companyCounter = 1;

    var conHolder = $(".contactsHolder");
    var viewHolder = $("#viewerHolder");

    var frontStop = conHolder.css("marginLeft");
    var ERROR = 0;

    var viewerHolderPadding = parseFloat($("#viewerHolder").css("padding-top"));
    var superViewerPadding = parseFloat($("#superViewer").css("padding-top"));
    var contactsHolderMargin = parseFloat($(".contactsHolder").css("margin-top"));
    var viewerHeight = parseFloat($(".viewer").css("height"));
    var contactsWidth = parseFloat($(".contacts").css("width"));
    var contactsMarginRight = parseFloat($(".contacts").css("margin-right"));
    var contactsMarginLeft = parseFloat($(".contacts").css("margin-left"));

    var moveHorzAmount = contactsWidth + contactsMarginRight + contactsMarginLeft;
    var moveVertAmount = viewerHeight + contactsHolderMargin;
    var detectViewerMeasure = viewerHolderPadding + superViewerPadding + contactsHolderMargin;

    var companyInput = "Company:<select><option>Wendys</option><option>McDonalds</option><option>Earls</option></select>-or-<input type='text' />";
    var contactInput = "<div class='formholder'><table border='0'><tr><td>Contact Name:</td><td><input type='text' /></td><td></td></tr><tr><td>Email:</td><td><input type='text' /></td><td></td></tr><tr><td>Position in Company:</td><td><input type='text' /></td><td></td></tr><tr><td>Phone number:</td><td><input type='text' /></td><td>Extension:</td><td><input type='text' /></td></tr><tr><td>Prime contact?</td><td><input type='checkbox' /></td><td></td></tr></table></div>";

    $("#controls").draggable({ opacity: .7 });

    function detectViewer(){

        var companies = [];
        var pos;

        viewHolder.children().each(function () {
            companies.push($(this));
        });

        for(var i = 0; i < companies.length; i++){

            pos = companies[i].position().top;

            if(pos == detectViewerMeasure){

                var idInPlace = companies[i].attr("id");

                return idInPlace.replace("viewer", "");

            }
        }

        alert("Failure in detectViewer() class, contact administrator");

        return -1;
    }

    function disableAll(){
        $("#right").attr("disabled", true);
        $("#left").attr("disabled", true);
        $("#up").attr("disabled", true);
        $("#down").attr("disabled", true);
        $("#rem").attr("disabled", true);
    }

    function enableAll(){
        $("#right").removeAttr("disabled");
        $("#left").removeAttr("disabled");
        $("#up").removeAttr("disabled");
        $("#down").removeAttr("disabled");
        $("#rem").removeAttr("disabled");
    }

    $("#left").click(function(){
        disableAll();

        var currentCompany = detectViewer();
        var toSlide = $("#holder" + currentCompany);
        var currentPosition = toSlide.css("marginLeft");

        if(currentPosition != frontStop || currentPosition < frontStop) {

            toSlide.animate({marginLeft:'+=' + moveHorzAmount + 'px'}, 'slow', function() {
                enableAll();
            });

        } else {
            enableAll();
        }
    });

    $("#right").click(function() {

        disableAll();

        var currentCompany = detectViewer();
        var toSlide = $("#holder" + currentCompany);
        var left = parseFloat(toSlide.css("marginLeft"));
        var width = parseFloat(toSlide.css("width"));

        if((left + width - moveHorzAmount) > moveHorzAmount){

            toSlide.animate({marginLeft:'-=' + moveHorzAmount + 'px'}, 'slow', function(){

                enableAll();

            });

        } else {
            enableAll();
        }
    });

    $("#down").click(function() {
        disableAll();

        var top = parseFloat(viewHolder.css("marginTop"));
        var height = parseFloat(viewHolder.css("height"));

        if((height+top) > moveVertAmount) {

            viewHolder.animate({marginTop: '-=' + moveVertAmount + 'px'}, 'slow', function( ){
                enableAll();
            });

        } else {
            enableAll();
        }
    });

    $("#up").click(function() {

        disableAll();
        var top = parseFloat(viewHolder.css("marginTop"));

        if(top < 0) {

            viewHolder.animate({marginTop: '+=' + moveVertAmount + 'px'}, 'slow', function(){
                enableAll();
            });

        } else {
            enableAll();
        }
    });     

    $("#add").click(function(){

        var viewerDetected = detectViewer();
        var tmpString = "#viewer" + viewerDetected + " > #contactsHolder";
        var companyAndContact = $(tmpString);
        var currentViewer = $("#holder" + viewerDetected);
        var size = currentViewer.css("width");
        var num = parseFloat(size);
        var px = "px";

        num += moveHorzAmount;  
        currentViewer.css("width", num);
        contactsCounter++;

        var tmpString = "#viewer" + viewerDetected + " >  #holder" + viewerDetected;
        var companyAndContact = $(tmpString);
        companyAndContact.append("<div class='contacts' id='con" + viewerDetected + "_" + contactsCounter + "'>" + contactInput + "</div>");
        currentViewer.animate({marginLeft:'-=' + moveHorzAmount + 'px'}, 'slow');

    });

    $("#addComp").click(function() {
        companyCounter++;
        var viewerDetected = detectViewer();
        viewHolder.append("<div class='viewer' id='viewer" + companyCounter + "'><div class='contactsHolder' id='holder" + companyCounter + "'><div class='contacts' id='con" + viewerDetected + "_" + "1'><div class='formholder'>" + companyInput + "</div></div></div></div>");
        viewHolder.animate({marginTop: '-=' + moveVertAmount + 'px'}, 'slow');

    });

    $("#rem").click(function(){

        disableAll();

        var kids = [];
        var currentCompany = detectViewer();
        var toRem = $("#holder" + currentCompany);

        toRem.children().each(function () {
            kids.push($(this));
        });

        var viewerView = (Math.round(parseFloat(toRem.css("marginLeft"))/moveHorzAmount))*-1;

        if(viewerView !== 0) {
            var currentID = kids[viewerView];
            var animateTo = parseInt($(".contacts").css("marginLeft"));

            toRem.animate({marginLeft:'+=' + moveHorzAmount + 'px'}, 'slow', function(){
                enableAll();
            });

            $(currentID).remove();

            var size = $("#holder" + currentCompany).css("width");
            var num = parseFloat(size);
            var px = "px";
            num -= moveHorzAmount;  
            toRem.css("width", num + px);

        } else {

            enableAll();
        }
    }); 

    $("#remComp").click(function() {
        disableAll();

        var currentCompany = detectViewer();
        var toRem = $("#viewer" + currentCompany);

        if(parseInt(currentCompany) !== 1){
            toRem.remove();

            viewHolder.animate({marginTop: '+=' + moveVertAmount + 'px'}, 'slow', function(){
                enableAll();
            });

        } else if(parseInt(currentCompany) == 1) {
            alert("You can't remove your only company!");
            enableAll();
        } else {
            enableAll();
        }
    }); 
});

2 个答案:

答案 0 :(得分:1)

  1. 检查脚本标记的MIME类型。应该是type =“text / javascript”
  2. 我没有看到调用函数slideRight()的代码。

答案 1 :(得分:0)

检查你是否有效地调用了slideRight() 检查 leftMarg fullWidth 的值,表达式“fullWidth + leftMarg&gt; opiWidth”可能不成立,因此您永远不会看到alert()。

此致