javascript and Internet Explorer 8

javascript and Internet Explorer 8

本文关键字:Explorer Internet and javascript      更新时间:2023-12-23

我有一个jsp页面,它使用JavaScript在onload页面事件中进行自动选择以及一些jQuery来动态扩展不同的div标签,其中包含一些内容。

因此,问题是,在IE8中,调用位于脚本标记中页面底部的setSelection方法和位于名为toggle的js方法中的jQuery方法根本不起作用。然而,在Chrome、Firefox和IE9中,它运行良好。

我在谷歌上搜索了很多这个问题,但找不到任何有用的东西。IE8中的调试器工具向我显示的唯一错误是"Object required"answers"Expression required"。

这是代码:

                        <html>
        <head>
            <script type="text/javascript" src="jquery-1.7.2.js"></script>
            <script type="text/javascript">
                function toggle(el)
                {
                    var val = el.options[el.selectedIndex].value;
                    if (val == "1")
                    {
                        document.getElementById("2").style.display = "none";
                        $("#1").slideToggle(250);
                    }
                    else if (val == "2")
                    {
                        document.getElementById("1").style.display = "none";
                        $("#2").slideToggle(250);
                    }
                    else if (val == "0")
                    {
                        document.getElementById("1").style.display = "none";
                        document.getElementById("2").style.display = "none";
                    }
                }
                function setSelection(selection) 
                {
                    //Here we make dynamic selection of the combo box
                    var selct = document.getElementById("sel");
                    selct.options[selection].setAttribute("selected", "selected");
                    toggle(selct);
                }
            </script>
        </head>
        <body>
        <select name="mySel" id = "sel" onchange="toggle(this);">
            <option value="0">Zero</option>
            <option value="1">One</option>
            <option value="2">Two</option>
        </select>
        <div id="1" style="border:1px;">
        </div>
        <div id="2" style="border:1px;">
        </div>
        <script>
            setSelection(0);
        </script>
        </body>
        </html>

有人能帮我吗?

将代码包装在文档就绪函数中并尝试。

jQuery(document).ready(function($) {
    // Stuff to do as soon as the DOM is ready. Use $() w/o colliding with other libs;
});