javascript TypeError:'undefined' 不是 Tablesorter 的函数,仅在 Safari 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22570548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:23:15  来源:igfitidea点击:

TypeError: 'undefined' is not a function with Tablesorter only in Safari

javascriptjquerysafaritablesorter

提问by skfjsdlfk

Only in safari I get the error:

只有在 safari 中,我才会收到错误消息:

TypeError: undefined is not a function (evaluating '$("table").tablesorter')

类型错误:未定义不是函数(评估 '$("table").tablesorter')

In all other browsers it works. This is my javascript code, and I have putt in the header the scripts to jquery and the tablesorter javascript. So how can i solve this problem? And why is it only in Safari and not in any other browser?

在所有其他浏览器中它都可以工作。这是我的 javascript 代码,我在标题中放入了 jquery 和 tablesorter javascript 的脚本。那么我该如何解决这个问题呢?为什么它只在 Safari 中而不在任何其他浏览器中?

 <script>

$(function() {

  // call the tablesorter plugin
$("table").tablesorter({
    theme : 'jui',
    headerTemplate : '{content}{icon}',
    // hidden filter input/selects will resize the columns, so try to minimize the 
          etc

采纳答案by Mottie

When jQuery is loaded twice, any scripts that are loaded between the two copies get associated with the first copy of jQuery (ref):

当 jQuery 被加载两次时,在两个副本之间加载的任何脚本都会与 jQuery 的第一个副本 ( ref)相关联:

<script src="jquery-copy1.js"></script>
<script src="myPluginExtendedFromJQ1.js"></script>

<script src="jquery-copy2.js"></script>
<script src="myPluginExtendedFromJQ2.js"></script>

<script>
// all of the jQuery's below are associated with jquery-copy2
jQuery(function(){
  // no problems
  jQuery('#demo-x').myPluginExtendedFromJQ2();

  // error: myPluginAttachedTOJQ1 is undefined
  jQuery('#demo-y').myPluginExtendedFromJQ1();
});
</script>

So once the document ready function is called, the jQuerycalls inside refer to the second copy of jQuery that was loaded.

因此,一旦文档就绪函数被调用,jQuery内部调用将引用已加载的 jQuery 的第二个副本。

If this situation is unavoidable, then you'll need to define a variable associated with the first copy:

如果这种情况不可避免,那么您需要定义一个与第一个副本关联的变量:

<script src="jquery-copy1.js"></script>
<script>var $jq1 = jQuery.noConflict();</script>
<script src="myPluginExtendedFromJQ1.js"></script>

<script src="jquery-copy2.js"></script>
<script src="myPluginExtendedFromJQ2.js"></script>

<!-- other stuff -->
<script>
// lets call plugins attached to the first copy of jQuery
// document ready can be called on either version $(function(){ ... })
jQuery(function(){
  // no problems
  jQuery('#demo-x').myPluginExtendedFromJQ2();

  // target first copy of jQuery
  $jq1('#demo-y').myPluginAttachedToJQ1();
});
</script>

Please refer to this jQuery forum postfor more details.

有关更多详细信息,请参阅此 jQuery 论坛帖子

In addition, I would report this issue to your web host, as they should be checking to see if jQuery already exists before loading it.

此外,我会将这个问题报告给您的网络主机,因为他们应该在加载之前检查 jQuery 是否已经存在。

回答by websky

in my case the solution was to:

就我而言,解决方案是:

1.scripting order

1.脚本顺序

jquery-1.11.1.js

jquery-latest.js

jquery.tablesorter.js

2.Method .ready(function($){}) - it is important to provide $

2.方法 .ready(function($){}) - 提供 $ 很重要

    jQuery(document).ready(function($){
            $("#tableSort").tablesorter(); 
    });

回答by Rodrigo Ranzini

This problem is because you're defining more than one jquery script. I had only this:

这个问题是因为您定义了多个 jquery 脚本。我只有这个:

<script src="@Url.Content("~/Scripts/jquery-1.11.3.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.tablesorter.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.tablesorter.pager.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.tablesorter.widgets.js")" type="text/javascript"></script>

And got the same error again and again.

并一次又一次地得到同样的错误。

Then I realized that the error was that! But I was using a Layout which defined another version of jquery. After changing the definition in my Layout the error was gone.

然后我意识到错误是这样的!但我使用的布局定义了另一个版本的 jquery。 在我的布局中更改定义后,错误消失了。

回答by Rites

I was also facing the same issue. In my case, sequence was the problem. The sequence adding jquery and tablesorter js is important. Load jquery first and then tablesorter.

我也面临同样的问题。就我而言,顺序是问题所在。添加 jquery 和 tablesorter js 的顺序很重要。首先加载 jquery,然后加载 tablesorter。

<script src="/js/jquery-1.11.0.min.js"></script> 
<script src="/js/jquery.tablesorter.min.js"></script>

earlier It was written as

早些时候它被写成

<script src="/js/jquery.tablesorter.min.js"></script>
<script src="/js/jquery-1.11.0.min.js"></script>

Hope it helps.

希望能帮助到你。