简体   繁体   English

Internet Explorer和jQuery问题

[英]Internet Explorer and jQuery issues

I have a script that works in Firefox, Safari, and Chrome. 我有一个可在Firefox,Safari和Chrome中运行的脚本。 It just doesn't work in Internet Explorer for whatever reason. 无论出于何种原因,它都无法在Internet Explorer中运行。 The code is fairly simple: 代码很简单:

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

    $.fn.tabSwap = function() {

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

    };

})( jQuery );
</script>

On a fairly simplified page (posted by Roatin Marth) the code worked just fine in IE 6 and IE 8. On my webpage the code does not work at all in Internet Explorer. 相当简化的页面上 (由Roatin Marth发布),该代码在IE 6和IE 8中都可以正常工作。在我的网页上,该代码在Internet Explorer中根本无法正常工作。

I tried executing the following simple code: 我尝试执行以下简单代码:

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

I get the following error: 我收到以下错误:

Object doesn't support this property or method 对象不支持此属性或方法

index.html line: 77 index.html行:77

code: 0 char: 2 代码:0字符:2

URI: ... URI:...

The link to my webpage is: 我网页的链接是:

http://examples.chikachu.com/calculators http://examples.chikachu.com/calculators

Any ideas what's wrong? 任何想法有什么问题吗?

Answer was posted by Crescent Fresh , but he isn't posting it as an answer so I can accept it. 答案由Crescent Fresh发布,但他没有将其发布为答案,所以我可以接受。 The problem on my site was improper closing of the <script> tag used to include the jQuery framework. 我网站上的问题是用于包含jQuery框架的<script>标签的不正确关闭。

More specifically, this issue. 更具体地说, 这个问题。

As a replacement for the JS you listed at the top, try this: 作为您在顶部列出的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>

Your plugin is not instantiated when you call it in IE -- try wrapping the call for your plugin in $() to make sure the DOM is ready (which theoretically should mean that your plugin has been downloaded and parsed [and therefore registered with jQuery.]) 当您在IE中调用插件时,您的插件未实例化-尝试将对插件的调用包装在$() ,以确保DOM准备就绪(从理论上讲,这意味着您的插件已下载并解析了[因此已在jQuery中注册。])

So change: 所以改变:

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM