我有一个简单的javascript文件,用于树状视图菜单中的复选框:
treeview。js公司
$(".acidjs-css3-treeview").delegate("label input:checkbox", "change", function() {
var
checkbox = $(this),
selectNestedListCheckbox = checkbox.parents("li").children("label").children("input"),
siblingCheckboxes = checkbox.parentsUntil("ul","li").children("label").children("input");
if(checkbox.is(":checked")) {
return selectNestedListCheckbox.prop("checked", true);
}
selectNestedListCheckbox.prop("checked", false);
});
我将此作为我的WP模板文件:
模板
get_header(); ?>
<?php
//create right sidebar template
kleo_switch_layout(\'right\');
?>
<?php get_template_part(\'page-parts/general-title-section\'); ?>
<?php get_template_part(\'page-parts/general-before-wrap\'); ?>
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post(); ?>
<!-- Begin the Treeview menu -->
<form method="get" action="<?php bloginfo(\'url\'); ?>">
<div class="form-group">
<input class="form-control" type="text" name="s" value="" placeholder="Search…" maxlength="50" required="required" />
</div>
<p>Refine search to posts containing chosen tags:</p>
<div class="acidjs-css3-treeview">
<ul>
<li><input type="checkbox" id="node-0" /><label><input type="checkbox" name="tag[]" value="node-0" /><span></span></label><label for="node-0">node-0</label>
<ul>
<li><input type="checkbox" id="node-0-0" /><label><input type="checkbox" name="tag[]" value="node-0-0" /><span></span></label><label for="node-0-0">node-0-0</label>
<ul>
<li><input type="checkbox" id="node-0-0-0" /><label><input type="checkbox" name="tag[]" value="node-0-0-0" /><span></span></label><label for="node-0-0-0">node-0-0-0</label></li>
<li><input type="checkbox" id="node-0-0-1" /><label><input type="checkbox" name="tag[]" value="node-0-0-1" /><span></span></label><label for="node-0-0-1">node-0-0-1</label></li>
</ul>
</li>
</ul>
</li>
<li><input type="checkbox" id="node-1" /><label><input type="checkbox" name="tag[]" value="node-1" /><span></span></label><label for="node-1">node-1</label>
<ul>
<li><input type="checkbox" id="node-1-0" /><label><input type="checkbox" name="tag[]" value="node-1-0" /><span></span></label><label for="node-1-0">node-1-0</label>
<ul>
<li><input type="checkbox" id="node-1-0-0" /><label><input type="checkbox" name="tag[]" value="node-1-0-0" /><span></span></label><label for="node-1-0-0">node-1-0-0</label></li>
<li><input type="checkbox" id="node-1-0-1" /><label><input type="checkbox" name="tag[]" value="node-1-0-1" /><span></span></label><label for="node-1-0-1">node-1-0-1</label></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<!-- End the Treeview menu -->
<input class="btn btn-primary" type="submit" value="Submit" />
</form>
<?php
function my_scripts_method() {
wp_enqueue_script(
\'custom-script\',
get_template_directory_uri() . \'-child/assets/js/treeview.js\',
array( \'jquery\' )
);
}
add_action( \'wp_enqueue_scripts\', \'my_scripts_method\' );
?>
<?php
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( \'content\', \'page\' );
?>
<?php get_template_part( \'page-parts/posts-social-share\' ); ?>
<?php if ( sq_option( \'page_comments\', 0 ) == 1 ): ?>
<!-- Begin Comments -->
<?php comments_template( \'\', true ); ?>
<!-- End Comments -->
<?php endif; ?>
<?php endwhile;
endif;
?>
<?php get_template_part(\'page-parts/general-after-wrap\'); ?>
<?php get_footer(); ?>
该模板的思想是允许用户根据搜索词进行搜索,并选择多个标记来过滤结果。
我有一个树状视图菜单设置,因此如果用户选择一个子标记,它也会自动选择其所有祖先。
Here is a jFiddle of what the menu would look and function like ideally.
这将导致标记未被过滤。I found this particular code for searching multiple tags here. 这个链接似乎表明,在WP 4.4出现之前,这是行不通的。在当前WP 4.3.1下,是否有办法使其工作?
SO网友:David Avellan
按照Milo的建议,我需要用jQuery替换$,如下所示:
treeview。js公司
jQuery(".acidjs-css3-treeview").delegate("label input:checkbox", "change", function() {
var
checkbox = jQuery(this),
selectNestedListCheckbox = checkbox.parents("li").children("label").children("input"),
siblingCheckboxes = checkbox.parentsUntil("ul","li").children("label").children("input");
if(checkbox.is(":checked")) {
return selectNestedListCheckbox.prop("checked", true);
}
selectNestedListCheckbox.prop("checked", false);
});
});
我将排队代码移到了函数中。php文件如下:
功能。php
function my_scripts_method() {
wp_enqueue_script(
\'custom-script\',
get_template_directory_uri() . \'-child/assets/js/treeview.js\',
array( \'jquery\' )
);
}
add_action( \'wp_enqueue_scripts\', \'my_scripts_method\' );
因此,我的模板html简化为:
模板
get_header(); ?>
<?php
//create right sidebar template
kleo_switch_layout(\'right\');
?>
<?php get_template_part(\'page-parts/general-title-section\'); ?>
<?php get_template_part(\'page-parts/general-before-wrap\'); ?>
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post(); ?>
<!-- Begin the Treeview menu -->
<form method="get" action="<?php bloginfo(\'url\'); ?>">
<div class="form-group">
<input class="form-control" type="text" name="s" value="" placeholder="Search…" maxlength="50" required="required" />
</div>
<p>Refine search to posts containing chosen tags:</p>
<div class="acidjs-css3-treeview">
<ul>
<li><input type="checkbox" id="node-0" /><label><input type="checkbox" name="tag[]" value="node-0" /><span></span></label><label for="node-0">node-0</label>
<ul>
<li><input type="checkbox" id="node-0-0" /><label><input type="checkbox" name="tag[]" value="node-0-0" /><span></span></label><label for="node-0-0">node-0-0</label>
<ul>
<li><input type="checkbox" id="node-0-0-0" /><label><input type="checkbox" name="tag[]" value="node-0-0-0" /><span></span></label><label for="node-0-0-0">node-0-0-0</label></li>
<li><input type="checkbox" id="node-0-0-1" /><label><input type="checkbox" name="tag[]" value="node-0-0-1" /><span></span></label><label for="node-0-0-1">node-0-0-1</label></li>
</ul>
</li>
</ul>
</li>
<li><input type="checkbox" id="node-1" /><label><input type="checkbox" name="tag[]" value="node-1" /><span></span></label><label for="node-1">node-1</label>
<ul>
<li><input type="checkbox" id="node-1-0" /><label><input type="checkbox" name="tag[]" value="node-1-0" /><span></span></label><label for="node-1-0">node-1-0</label>
<ul>
<li><input type="checkbox" id="node-1-0-0" /><label><input type="checkbox" name="tag[]" value="node-1-0-0" /><span></span></label><label for="node-1-0-0">node-1-0-0</label></li>
<li><input type="checkbox" id="node-1-0-1" /><label><input type="checkbox" name="tag[]" value="node-1-0-1" /><span></span></label><label for="node-1-0-1">node-1-0-1</label></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<!-- End the Treeview menu -->
<input class="btn btn-primary" type="submit" value="Submit" />
</form>
<?php
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( \'content\', \'page\' );
?>
<?php get_template_part( \'page-parts/posts-social-share\' ); ?>
<?php if ( sq_option( \'page_comments\', 0 ) == 1 ): ?>
<!-- Begin Comments -->
<?php comments_template( \'\', true ); ?>
<!-- End Comments -->
<?php endif; ?>
<?php endwhile;
endif;
?>
<?php get_template_part(\'page-parts/general-after-wrap\'); ?>
<?php get_footer(); ?>
通过这些修改,现在javascript可以正常运行。我仍然需要修复标记搜索,但我会问另一个问题。