JavaScript和Internet Explorer 8

问题描述:

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

所以问题是调用setSelection方法,这是在页脚底部的脚本标记,以及在js方法中调用jQuery方法调用toggle在IE8中完全不起作用。但是在Chrome,Firefox和IE9中,它工作正常。

我已经谷歌搜索这个问题很多,但我找不到任何有用的东西。 IE8中的debuger工具显示的唯一错误是“需要对象”和“需要表达式”。

下面是代码:

     <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> 

有人可以帮我吗?

+2

转到计算机的代码,并张贴上的jsfiddle所以我们可以帮你的 – 2012-07-20 16:50:32

+0

你有一个链接到该网站或它只有本地? – 2012-07-20 16:50:50

+0

@Rey他也应该在这里发布。链接到第三方网站是一个坏主意,因为它不能确定它们会永远持续下去。如果jsFiddle消失了,这个问题对未来的读者来说没有任何意义。 – 2012-07-20 16:51:52

将您的代码包装在文档准备好的功能中,然后尝试。

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

它工作正常,现在我必须了解为什么div标签的内容不想显示。谢谢 :) – 2012-07-20 17:33:32