如何在动态php文件中使用toggle()函数切换每个div

时间:2019-02-18 23:26:00

标签: php jquery html5 css3

我正在最小化列表块的视觉显示,并且我想使用带按钮的切换来隐藏我不想默认显示的内容。

我几乎使用next()jQuery属性来执行此操作,但这迫使我将按钮放在要隐藏的内容的上一个元素中(这就是为什么将其称为NEXT),但是我想隐藏类内容下面的两个或三个类元素。我该怎么做?我尝试了siblings(),但没有用。

        if ( tribe_get_cost() ) : 
            $event_cost='<!-- Event Cost -->
            <div class="ect-event-cost">
                <span>'.tribe_get_cost($event_id, true ).'</span>
            </div>';
            endif;

            $event_title='<a class="ect-event-url" href="'.esc_url( tribe_get_event_link()).'" rel="bookmark">'. get_the_title().'</a>';


            $event_content='<button class="btn1" class="button">More..</button><!-- Event Content --><div class="p1"><div class="ect-event-content">';
              $event_content.=tribe_events_get_the_excerpt($event_id, wp_kses_allowed_html( 'post' ) );

            $event_content.='<a href="'.esc_url( tribe_get_event_link($event_id) ).'" class="ect-events-read-more" rel="bookmark">'.esc_html__( 'Find out more', 'the-events-calendar' ).' &raquo;</a></div></div></div>';
    /*
$(document).ready(function () {
    $(".p1").hide();

    $(".btn1").click(function(){
        $(this).next(".p1").toggle()
    });

  });

1 个答案:

答案 0 :(得分:1)

您知道为什么.next()在使用$(this).next(".p1").toggle()时不起作用吗?

因为您之间有一条评论:<!-- Event Content -->

它将代替。注释标签。只需将其删除,然后重试。

由于您使用的是PHP ...建议您将这种注释放在如下这样的PHP注释中:<?php // Event Content ?>,这样它就不会显示在客户端的渲染页面中。