javascript - 类型错误 : 'undefined' is not a function with Tablesorter only in Safari

标签 javascript jquery safari tablesorter

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

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

在所有其他浏览器中它都有效。 这是我的 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

最佳答案

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

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

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

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

请引用this jQuery forum post了解更多详情。

此外,我会向您的虚拟主机报告此问题,因为他们应该在加载 jQuery 之前检查它是否已经存在。

关于javascript - 类型错误 : 'undefined' is not a function with Tablesorter only in Safari,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22570548/

相关文章:

javascript - 有没有办法在圆环 Chart.JS 中显示 100 中的 %

javascript - dragBoundFunc 中的鼠标位置

javascript - 图像轮播波动且未给出预期结果

javascript - 使用文档写入异步/延迟或页面加载后加载广告服务器脚本

javascript - jquery 只获取 tr 的第一个复选框

ios - HTML标签打不开用safari调试ipad

javascript - 数组中的 angularjs 对象上没有 $$hashKey

javascript - JS倒计时清除文本文件

javascript - 有什么方法可以防止水平滚动触发 OS X Lion Safari 上的向后滑动手势?

html - 通过 UIWebView 从网页中在 Safari 中打开链接