首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换UL-> List列表,用于同位素过滤菜单

转换UL-> List列表,用于同位素过滤菜单
EN

Stack Overflow用户
提问于 2015-02-23 09:29:30
回答 1查看 772关注 0票数 0

我的同位素过滤器菜单有问题。我想转换一个选择的列表框中的简单链接,但是我对JS有问题,或者我真的不知道Ul->Li是否可行。

下面是选择的菜单

代码语言:javascript
复制
jQuery(function ($) {
 
	var $container = $('#posts-list');
	$container.isotope({
		itemSelector : '.item', 
  		layoutMode : 'masonry'
	});
 
	var $optionSets = $('#filters'),
	$optionLinks = $optionSets.find('a');
 
	$optionLinks.click(function(){
	var $this = $(this);
	if ( $this.hasClass('selected') ) {
	  return false;
	}
	var $optionSet = $this.parents('#filters');
	$optionSets.find('.selected').removeClass('selected');
	$this.addClass('selected');
 
	 var selector = $(this).attr('data-filter');
	$container.isotope({ filter: selector });
 
	return false;
	});
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="filters">
    <li><a href="#" data-filter="*" id="all">Everything</a></li>
	<?php 
		$terms = get_terms("category");
		$count = count($terms);
		if ( $count > 0 ){
			foreach ( $terms as $term ) {
				echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
			}
		} 
	?>
</ul>

///////////////////
DISPLAY ITEMS
///////////////////

<div id="posts-list">
  		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
			$termsArray = get_the_terms( $post->ID, "category" );
			$termsString = "";
			foreach ( $termsArray as $term ) {
				$termsString .= $term->slug.' ';
			} ?> 
			<div class="<?php echo $termsString; ?> item bit-3">
               // DISPLAY THE CONTENT
			</div>
  		<?php endwhile;  ?>
  	</div>

我试过选择的菜单:

代码语言:javascript
复制
jQuery(function ($) {
 
	var $container = $('#posts-list');
	$container.isotope({
		itemSelector : '.item', 
  		layoutMode : 'masonry'
	});
  
$("#filters").on("click", ".init", function() {
    $(this).closest("ul").children('li:not(.init)').toggle();
});

var allOptions = $("ul").children('li:not(.init)');
$("ul").on("click", "li:not(.init)", function() {
    allOptions.removeClass('selected');
    $(this).addClass('selected');
    $("ul").children('.init').html($(this).html());
    allOptions.toggle();
});

  });
代码语言:javascript
复制
<ul id="filters">
    <li class="init"><a href="#" data-filter="*" id="all">Everything</a></li>
	<?php 
		$terms = get_terms("category");
		$count = count($terms);
		if ( $count > 0 ){
			foreach ( $terms as $term ) {
				echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
			}
		} 
	?>
</ul>

你能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-23 11:30:09

下面的代码可以做到这一点:

代码语言:javascript
复制
<select id="filters">
     <option data-filter="*" id="all">Everything</option>
    <?php 
        $terms = get_terms("category");
        $count = count($terms);
        if ( $count > 0 ){
            foreach ( $terms as $term ) { 
                echo "<option data-filter='.".$term->slug."'>" . $term->name . "
                         </option>\n";
            }
        } 
    ?>
</select>

<?php $the_query = new WP_Query( 'posts_per_page=50' );
    if ( $the_query->have_posts() ) : ?>
    <div id="posts-list">
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
           $termsArray = get_the_terms( $post->ID, "category" ); ?> 
          <!-- Get the terms for this particular item -->
    <?php 
        $termsString = "";
        foreach ( $termsArray as $term ) {
            $termsString .= $term->slug.' ';
        } ?> 
    <div class="<?php echo $termsString; ?>item">
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <h1>
                <a href="<?php the_permalink() ?>">
                    <?php the_title(); ?>
                </a>
            </h1>
            <section class="post_content">
                <p><?php echo get_the_content(); ?></p>
            </section>
        </article>
    </div> <!-- end item -->
    <?php endwhile;  ?>
</div> <!-- end isotope-list -->
<?php endif; ?>

JS

代码语言:javascript
复制
jQuery(function ($) {

    var $container = $('#posts-list'),
    filters = {};

    $container.isotope({
        itemSelector : '.item'
    });

    // filter buttons
    $('select').change(function(){
        var $this = $(this);

        var group = $this.attr('data-filter');

        filters[ group ] = $this.find(':selected').attr('data-filter');

        var isoFilters = [];
        for ( var prop in filters ) {
            isoFilters.push( filters[ prop ] )
        }

        var selector = isoFilters.join('');
        $container.isotope({ filter: selector });
        return false;
    });

});

注:测试

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28670527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档