【问题标题】:Are there any jquery conflicts in my wordpress code?我的 wordpress 代码中是否有任何 jquery 冲突?
【发布时间】:2019-03-11 23:33:18
【问题描述】:

我有一个 wordpress 页面,我想在其中使用数据值检查复选框中的类别。在本例中为“公告”

这个脚本在 codepen 上运行得很好,但在 wordpress 上不行。以下是我放在 wordpress 页面上的所有内容。

jQuery(document).ready(function() {
  var input = $('input[data-value="Announcement"]')
  input.focus();
  input.select();
  input.prop('checked', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<input type="checkbox" id="test" name="category[]" data-wpt-type="checkbox" data-wpt- id="test" data-wpt- name="category[]" value="1" data-parent="-1" data-value="Announcement" class="wpt-form-checkbox form-checkbox checkbox">

<input type="checkbox" id="test2" name="category[]" data-wpt-type="checkbox" data-wpt- id="test2" data-wpt- name="category[]" value="2" data-parent="-1" data-value="Story" class="wpt-form-checkbox form-checkbox checkbox">

这是一个 jquery 问题/冲突吗?是主题冲突,还是代码错误?

jQuery.Deferred exception: 
b(...).not(...).filter(...).mediaelementplayer is not a function 
a@http://localhost/wordpress2018/wp-includes/js/mediaelement/wp- 
mediaelement.min.js?ver=4.9.8:1:634 g/</k<@https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2: 
30262
 undefined jquery.min.js:2:31515
jQuery.Deferred exception: jQuery(...).css(...).mCustomScrollbar is 
not a 
function Init_BodyConv@http://localhost/wordpress2018/wp- 
content/plugins/um-messaging/assets/js/um-messaging.js?ver=4.9.8:36:2
@http://localhost/wordpress2018/wp-content/plugins/um- 
messaging/assets/js/um-messaging.js?ver=4.9.8:115:2 j@https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:2994 g/</k<@https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:30262

【问题讨论】:

  • 检查控制台是否有错误。另请注意,您只需要 prop() 行来设置选中状态。
  • 我添加了一些我认为相关的控制台错误,我仍然是菜鸟,那里有什么指向问题的地方吗?

标签: javascript jquery wordpress checkbox content-management-system


【解决方案1】:

Wordpress 主题通常使用 jQuery.noConflict(),这会使全局 $ 未定义,以防止与也可能使用 $ 的其他库发生冲突

尝试改变

jQuery(document).ready(function() {

jQuery(document).ready(function($) {
                             // ^^

这将在ready 回调中公开$

【讨论】:

  • 建议编辑:which makes global $ undefined to prevent conflicts with other libraries -> which makes global $ undefined to drive developers crazy
  • 那么浏览器开发工具控制台会抛出什么错误呢?
  • 尝试将$ = jQuery; 添加为函数的第一行。疯狂,但经常为我解决!
【解决方案2】:
  1. 尝试删除jquery CDN
  2. 手动下载 jquery.min.js
  3. 不要将相关的 jquery/js 文件从 function.php 中加入队列,而是像 &lt;script src="&lt;?php echo get_bloginfo( 'template_directory' ); ?&gt;/js/jquery.min.js"&gt;&lt;/script&gt; 这样包含在头部中
  4. 然后将给出函数的js文件包含在jquery include下面

这解决了我的问题。

【讨论】:

    【解决方案3】:

    尝试重新安装或升级 wordpress 安装,它帮助我解决了这个 mediaelementplayer.js 错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-30
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多