自定义JQuery在wordpress中不起作用 [英] Custom JQuery is not working in wordpress

查看:69
本文介绍了自定义JQuery在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆