首页 > 解决方案 > 我的 wordpress 代码中是否有任何 jquery 冲突?

问题描述

我有一个 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

标签: javascriptjquerywordpresscheckboxcontent-management-system

解决方案


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

尝试改变

jQuery(document).ready(function() {

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

这将$ready回调中公开


推荐阅读