【问题标题】:Internet Explorer and jQuery issuesInternet Explorer 和 jQuery 问题
【发布时间】:2010-10-26 17:23:19
【问题描述】:

我有一个可在 Firefox、Safari 和 Chrome 中运行的脚本。无论出于何种原因,它都无法在 Internet Explorer 中运行。代码相当简单:

<script type="text/javascript">
(function( $ ){

    $.fn.tabSwap = function() {

        return this.each(function() {
            $('.current').removeClass('current');
            $(this).addClass('current');
        });

    };

})( jQuery );
</script>

a fairly simplified page(由 Roatin Marth 发布)上的代码在 IE 6 和 IE 8 中运行良好。my webpage 上的代码在 Internet Explorer 中根本无法运行。

我尝试执行以下简单代码:

<script type="text/javascript">
    $('#statistics').tabSwap();
</script>

我收到以下错误:

对象不支持此属性或方法

index.html 行:77

代码:0 字符:2

URI: ...

我网页的链接是:

http://examples.chikachu.com/calculators

有什么想法吗?

【问题讨论】:

  • 工作正常:jsbin.com/ulaho#noedit(在 IE6 中测试)。你在看什么?
  • @Roatin Huh...你发给我的链接在 IE8 中工作。我的网站一定有一些更复杂的东西。我会上传我的网站,重新测试,如果仍然无法使用,我会发布一个链接。
  • @Crescent 解决了问题,谢谢 =) 请作为答案发布,以便我可以将其标记为已接受的答案。

标签: javascript jquery internet-explorer


【解决方案1】:

答案由Crescent Fresh 发布,但他并未将其发布为答案,因此我可以接受。我网站上的问题是不正确地关闭了用于包含 jQuery 框架的 &lt;script&gt; 标记。

更具体地说,this issue.

【讨论】:

    【解决方案2】:

    当你在 IE 中调用你的插件时,它没有被实例化——尝试在 $() 中包装你的插件调用,以确保 DOM 已准备好(这理论上应该意味着你的插件已被下载并解析 [因此用 jQuery 注册。])

    所以改变:

    <script type="text/javascript">
        $('#tabTwo').tabSwap();
    </script>
    

    <script type="text/javascript">
        $(function() { 
            $('#tabTwo').tabSwap(); 
        });
    </script>
    

    【讨论】:

    • 这个方法和之前一样报错,只是报错两次。
    【解决方案3】:

    作为您在顶部列出的 JS 的替代品,试试这个:

    <script type="text/javascript">
    jQuery.fn.tabSwap = function() {
    
            return this.each(function() {
                $('.current').removeClass('current');
                $(this).addClass('current');
            });
    
    };    
    
    $(document).ready(function() {
            $('#tabTwo').tabSwap();
    });
    </script>
    

    【讨论】:

    • 你不应该在 document.ready 处理程序中声明插件。
    • 当我尝试这个时,什么也没发生。没有错误,但选项卡从未交换过(在 IE 中)。
    • @steven_desu 我更新了答案。我能够使用 chrome 控制台在您的示例网站上完成这项工作。
    • 问题:它在 Chrome 中工作。它在 IE 中不起作用。 Crescent Fresh 发现了这个问题。我只是在等待他以答案形式发布,以便我接受。
    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多