IE正在使用JQuery做一些奇怪的事情

时间:2010-05-05 11:56:31

标签: javascript jquery internet-explorer firefox

所以...问题是,代码在FireFox中运行,没问题。但是当我打开同一页面时,它会给我以下错误:

“Undefined为null或不是对象。”

但是当我将代码复制到localhost页面时,它工作正常。 此外,当我在IE中清除缓存时,它可以工作,但只有一次,如果我在一次加载后刷新,它会给我同样的错误。

以下是代码:

<script type="text/javascript" src="datepicker/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
     var count3 = 0; 
     var count5 = 0;
     var count2 = 0;
     var count4 = 0;

    $(document).ready(function(){

    $('#switch3').click(function(){
        $('#switchDiv3').slideToggle(350);
            if(count3 == 0){
            count3 = 1;
            document.getElementById('switchImage3').src = "images/ArrowDown.png";
            return;
            } else {
            count3 = 0;
            document.getElementById('switchImage3').src = "images/ArrowRight.png";
            return;
            }
    });

    ... (this is the code for each item that is generated) 
    </script>

确定应该隐藏的div的代码:

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
        <td width="20" align="center" valign="top" style="padding-right: 3px">
            <a style="cursor: pointer;" id="switch3"><img width="20" height="20" src="images/ArrowRight.png" id="switchImage3" style="border-style: solid; border-width: 1px; border-color: black;"/></a>
        </td>
        <td>
            <div id="switchDiv3">
                <div align="left">
                    (Contents of the div here)
                </div>
            </div>
        </td>
    </tr>
</table>

感谢任何帮助!

提前致谢

3 个答案:

答案 0 :(得分:0)

您需要描述您想要做的事情。我想你想要显示和隐藏div并在你做的时候从左到右切换箭头。

你可以使用切换功能,它非常酷,下面是你想要的东西的例子(我猜)。

<html>
<head>
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            $("#switch3").toggle(function(){
                $("#switch3").html('<-');
                $("#switchDiv3").slideToggle(350);;

            }, function(){
                $("#switch3").html('->');
                $("#switchDiv3").slideToggle(350);
            });
        });
    </script>
</head>
<body>
    <div>
    <a id="switch3" style="cursor: pointer;"> -> </a>
        <div id="switchDiv3" style="background-color: red">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        </div>
    </div>
</body>

顺便说一下,我只使用内联样式作为示例,你应该使用外部样式表。

答案 1 :(得分:0)

所以......

原型功能也不起作用,没有任何线索为什么...... 所以现在我要尝试更改显示属性。

也没有运气。脚本会更改箭头,但它不会隐藏div ...

以下是代码:

var count3 = 0;
function showHideDiv3(){
    if(count3 == 0){
        document.getElementById('switchDiv3').style.display = '';
        document.getElementById('switchImage3').src = "images/ArrowDown.png";
        count3 = 1;
    }else{
        if(count3 == 1){
            document.getElementById('switchDiv3').style.display = 'block';
            document.getElementById('switchImage3').src = "images/ArrowRight.png";
            count3 = 0;
        }
    }
}

div的名称是正确的。我已经检查了,我的三位同事也进行了双重检查。

这让我疯狂和沮丧lol。

答案 2 :(得分:0)

您是否只是尝试交换图标来指示状态?如果是这样,只需选择放置在图像上的id的src属性并交换图形。

所以,使用类似的东西:

$('#swtichImage3').hover(function() {
$(this).attr('src', 'images/ArrowDown.png');
}, function(){
$(this).attr('src', 'images/ArrowRight.png');
});

在该示例中,我正在使用hover()动作,但您可以交换它。我只想把它放在一个更简单的交换src属性的方法上。