自定义jQuery不是在WordPress中不起作用的代码

时间:2017-01-11 作者:nehaJ

我需要在单击时显示子菜单,我试图通过自定义j查询来完成。

我有这样的菜单结构,

<ul class="vc_grid-filter vc_clearfix vc_grid-filter- vc_grid-filter-size-md vc_grid-filter-center vc_grid-filter-color-" data-vc-grid-filter="product_cat">
    <li class="vc_active vc_grid-filter-item parent"><a class="dropdown-toggle"><span data-vc-grid-filter-value=".vc_grid-term-61">Product by Doctors</span></a>
        <!-- fix whitespace
                -->
    </li>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-62">GP</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-63">Paediatrics</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-64">Physician</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-65">Orthopaedic</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-66">Gynaecology</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-67">Dentists</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-68">General Surgeon</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-69">ENT</span></li>
    </ul>
    <li class="vc_active vc_grid-filter-item parent"><a class="dropdown-toggle"><span data-vc-grid-filter-value=".vc_grid-term-60">Products by Category</span></a>
        <!-- fix whitespace
                -->
    </li>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-70">Anti Biotic</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-71">Anti-Inflammatory</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-72">Tonics</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-73">Anti Cold and Expectorants</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-74">Antacids and Antiulcer</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-75">Ointments</span></li>
    </ul>
    <ul class="vc_grid-filter-item child">
        <li><span data-vc-grid-filter-value=".vc_grid-term-76">Gastro-Intestinal</span></li>
    </ul>
    <li class="vc_grid-filter-item"><a class="dropdown-toggle"><span data-vc-grid-filter-value="*">Complete Product List</span></a></li>
</ul>
在主题的js文件夹中,我创建了名为drop的jquery文件。js。这是代码

var $j = jQuery.noConflict();
$j(document).ready(function() {
    $j(".child").hide();
    $("li:has(ul)").click(function(){

        $("ul",this).toggle(\'slow\');
    });
});
和在功能上。我添加的php文件代码

//这在函数中进行。顶部附近的php

function my_scripts_method() {
// register your script location, dependencies and version
    wp_register_script( \'custom_script\', get_template_directory_uri() . \'/js/drop.js\', array(\'jquery\'), \'1.0\' );
// enqueue the script
    wp_enqueue_script(\'custom_script\');
}
add_action(\'wp_enqueue_scripts\', \'my_scripts_method\');
你能检查一下这个代码并告诉我哪里错了吗。它不起作用。谢谢

1 个回复
SO网友:codiiv

问题在于html结构。应该是这样的

<ul>
   <li>
      <ul>
        <li><a href="">Somwthing</a></li>
      </ul>
  </li>
</ul>

相关推荐