简体   繁体   English

自定义JQuery在wordpress中不起作用

[英]Custom JQuery is not working in wordpress

I have written JQuery for my website search functionality. 我已经为网站搜索功能编写了JQuery。 Its working fine when I am using separate file. 当我使用单独的文件时,它的工作正常。 But when I included same JQuery file in wordpress its not working as required. 但是当我在wordpress中包含相同的JQuery文件时,它无法按要求工作。 My question is how to include this code in wordpress 我的问题是如何在Wordpress中包含此代码

Code: 码:

$('#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();
     });
 });

Firstly, ensure you have WordPress loading onto your template. 首先,请确保将WordPress加载到模板上。

In your head file, ensure you have this loaded in 在头文件中,确保已将其装入

<?php wp_enqueue_script("jquery"); ?>

Next, WordPress has to use noConflict mode or use the base jQuery call instead of $ . 接下来,WordPress必须使用noConflict模式或使用基本jQuery调用而不是$

Try this 尝试这个

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();
    });
});

You should also ensure that all your jQuery code is wrapped in the following code: 您还应该确保所有jQuery代码都包装在以下代码中:

jQuery(document).ready(function() {
    //code to run here
});

You have to try some trial and error method in fact.It can be because of jquery conflicts. 实际上,您必须尝试一些试错法,这可能是由于jquery冲突引起的。

These are the possibilities coming in mind 这些是即将到来的可能性

  1. Try placing your jQUERY code above and below of wp_head(); 尝试将jQUERY代码放在wp_head();上方和下方wp_head();

  2. Check if you are using the latest version of jquery file( or check if you use the same jquery file of the working file ) 检查您是否正在使用最新版本的jquery文件(或检查您是否使用了与工作文件相同的jquery文件)

  3. Try placing your jQUERY code above and below of jquery file 尝试将jQUERY代码放在jquery文件的上方和下方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM