【问题标题】:Uncaught TypeError: $.widget is not a function in Wordpress Theme未捕获的类型错误:$.widget 不是 Wordpress 主题中的函数
【发布时间】:2016-01-23 11:43:57
【问题描述】:

我打算在主题中使用 jQuery 自动完成组合。在 header.php 中加载如下脚本文件

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

当我尝试它时,我得到了 javascript 错误,因为 Uncaught TypeError: $.widget is not a function。这是 jquery ui 自动完成 demo ,我在 JSFiddle 上尝试过,也可以作为演示。那是JSFiddle 工作演示。我在 wordpress 主题中添加 $.widget 错误。我该如何解决它

【问题讨论】:

  • 你应该使用 wordpress 的 enqueue 脚本函数来确保脚本等以正确的(依赖!)顺序加载。

标签: jquery-ui jquery-ui-autocomplete wordpress


【解决方案1】:

你需要在主题的functions.php中调用jquery ui小部件

wp_enqueue_script('jquery-ui-widget');

【讨论】:

  • 之前尝试过,不会出现 javascript 错误,但它不起作用。所以我决定用上述风格调用并得到错误
  • 尝试使用 'jQuery' 变量而不是 '$' ,因为 wordpress 中的 jquery 是以无冲突模式加载的
  • 根据本教程devotepress.com/tutorials-hacks/wordpress-coding/… 我正在使用正确的方法并得到**$.widget** 不是一个函数。当我尝试用 jQuery 替换 _$_ 时,我得到 jQuery.widget 不是一个函数。还有什么办法可以解决吗?
  • 当我尝试使用此方法/*! * Coding jQuery Proper Way * using wrapper * to be executed after the document has been loaded */ jQuery(document).ready(function($) { // Inside of this function, $() will work as an alias for jQuery() // and other libraries also using $ will not be accessible under this shortcut }); 时,我没有收到任何错误但我认为 $.widget 函数不再起作用了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多