自定义JQuery在wordpress中不起作用 [英] Custom JQuery is not working in wordpress
问题描述
我已经为网站搜索功能编写了JQuery.当我使用单独的文件时,它的工作正常.但是当我在wordpress中包含相同的JQuery文件时,它无法按要求工作. 我的问题是如何在wordpress中包含此代码
I have written JQuery for my website search functionality. Its working fine when I am using separate file. But when I included same JQuery file in wordpress its not working as required. My question is how to include this code in wordpress
代码:
$('#search').keyup(function () {
var filter = $('#search').val();
$('.et_pb_row').each(function() {
$(this).find("h3:not(:contains('" + filter + "'))").parent().hide();
$(this).find("h3:contains('" + filter + "')").parent().show();
});
});
推荐答案
首先,确保将WordPress加载到模板上.
Firstly, ensure you have WordPress loading onto your template.
在头文件中,确保已将其加载
In your head file, ensure you have this loaded in
<?php wp_enqueue_script("jquery"); ?>
下一步,WordPress必须使用noConflict
模式或使用基本jQuery
调用而不是$
.
Next, WordPress has to use noConflict
mode or use the base jQuery
call instead of $
.
尝试一下
jQuery('#search').keyup(function () {
var filter = jQuery('#search').val();
jQuery('.et_pb_row').each(function() {
jQuery(this).find("h3:not(:contains('" + filter + "'))").parent().hide();
jQuery(this).find("h3:contains('" + filter + "')").parent().show();
});
});
您还应该确保所有jQuery代码都包装在以下代码中:
You should also ensure that all your jQuery code is wrapped in the following code:
jQuery(document).ready(function() {
//code to run here
});
这篇关于自定义JQuery在wordpress中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!