【问题标题】:Uncaught TypeError: $(...).tooltip is not a function error未捕获的 TypeError: $(...).tooltip 不是函数错误
【发布时间】:2016-03-21 08:08:48
【问题描述】:

我正在尝试将此代码与这些工具提示一起使用。在此链接中:Code with tooltip。事实上,我只是复制了整个代码,但它现在可以工作了。 我收到一条错误消息,上面写着Uncaught TypeError: $(...).tooltip is not a function error:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">

<script>
  $(function() {
    $(document).tooltip({
      position: {
        my: "center bottom-20",
        at: "center top",
        using: function(position, feedback) {
          $(this).css(position);
          $("<div>")
            .addClass("arrow")
            .addClass(feedback.vertical)
            .addClass(feedback.horizontal)
            .appendTo(this);
        }
      }
    });
  });
</script>

这里缺少什么?

编辑:

<script>
jQuery( document ).ready(function( $ ) {
   $('.hasTooltip').tooltip();
});
</script>

这样使用,解决了我的问题!

【问题讨论】:

  • jsbin.com/kuteqafele/1/edit?html,output — 我无法重现该问题。
  • 为什么要加载 jQuery ui 两次?
  • @JuanMendes 你的意思是?
  • @JuanMendes,一个是css,一个是js
  • @Sanchit — 在编辑问题之前加载了两个版本的 jQuery UI。

标签: javascript jquery css jquery-ui


【解决方案1】:

您包含两倍于不同版本的 jQuery UI。这可能会导致冲突。尝试删除

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>

【讨论】:

  • 即使我编辑了我的问题,仍然是同样的问题,还有其他想法吗?
  • 不是我的反对意见,但像“试试这个,试试那个”这样的猜测应该是评论
【解决方案2】:

您收到错误是因为在您的脚本执行后加载了 jquery-ui。 您可以尝试将 type="text/javascript" 通常添加到您的脚本中,这必须解决问题 或者您必须等到脚本加载后才能执行您的工具提示。所以你必须创建一个函数,在加载 jquery-ui 后执行你的脚本。

【讨论】:

  • “你收到错误是因为 jquery-ui 是在你的脚本执行后加载的”——这不是问题中的代码显示的内容。