【问题标题】:Meteor Live Accordion MenuMeteor 现场手风琴菜单
【发布时间】:2016-08-27 12:24:53
【问题描述】:

我正在创建一个社交媒体风格的网站,并使用 Meteor 和 MongoDB 创建帖子,{{title}}{{content}} 在手风琴菜单内,标题位于菜单标题上,内容位于切换手风琴菜单。

我的代码如下所示:

HTML:

<template name="postsList">

{{#each posts}}



<div class="accordion">
<div class="accordion-section">
    <a class="accordion-section-title" href="#accordion-1">{{title}}</a>

    <div id="accordion-1" class="accordion-section-content">
        <p>{{content}}</p>
    </div><!--end .accordion-section-content-->
</div><!--end .accordion-section-->
</div><!--end .accordion-->


{{/each}}

</template>

JS:

    function close_accordion_section() {
    $('.accordion .accordion-section-title').removeClass('active');
    $('.accordion .accordion-section-content').slideUp(300).removeClass('open');
}

$('.accordion-section-title').click(function(e) {
    // Grab current anchor value
    var currentAttrValue = $(this).attr('href');

    if($(e.target).is('.active')) {
        close_accordion_section();
    }else {
        close_accordion_section();

        // Add active class to section title
        $(this).addClass('active');
        // Open up the hidden content panel
        $('.accordion ' + currentAttrValue).slideDown(300).addClass('open'); 
    }

    e.preventDefault();
});

CSS:

/*----- Accordion -----*/
.accordion, .accordion * {
-webkit-box-sizing:border-box; 
-moz-box-sizing:border-box; 
box-sizing:border-box;
}

.accordion {
overflow:hidden;
box-shadow:0px 1px 3px rgba(0,0,0,0.25);
border-radius:3px;
background:#f7f7f7;
}

/*----- Section Titles -----*/
.accordion-section-title {
width:100%;
padding:15px;
display:inline-block;
border-bottom:1px solid #1a1a1a;
background:#333;
transition:all linear 0.15s;
/* Type */
font-size:1.200em;
text-shadow:0px 1px 0px #1a1a1a;
color:#fff;
}

.accordion-section-title.active, .accordion-section-title:hover {
background:#4c4c4c;
/* Type */
text-decoration:none;
}

.accordion-section:last-child .accordion-section-title {
border-bottom:none;
}

/*----- Section Content -----*/
.accordion-section-content {
padding:15px;
display:none;
}

但是虽然{{title}}显示正确,但当你点击它时(应该打开{{content}})它没有,尽管MongoDB数据库中的内容是正确的,为什么会发生这种情况?

任何帮助将不胜感激!

语义 UI 代码:

$(document).ready(function(){   
$('.ui.accordion')
.accordion();
});

 <template name="postsList">

 {{#each posts}}

<div class="ui accordion">
  <div class="active title">
 <i class="dropdown icon"></i>
 {{title}}
 </div>
 <div class="content">
  {{content}}
  </div>
  </div>
{{/each}}

  </template>

【问题讨论】:

    标签: javascript jquery html css meteor


    【解决方案1】:

    javascript 应该在events 块中

    Template.postlist.events({
          "click .accordion-section-title" : function(){
              // your code here
    
          } 
    
    
     })
    

    见这个例子Meteor Todo Tutorial

    或者,使用 twbs:bootstrapsemantic:ui 包并将其内置函数用于手风琴。

    【讨论】:

    • 我决定使用 Semantic: UI 不幸的是,虽然标题显示在手风琴上,但手风琴功能不起作用,所以内容永远不会显示。我只是简单地把 {{title}} 和 {{content}} 都显示出来了,是不是发生了什么奇怪的事情?
    • 您需要在渲染块中初始化手风琴。
    • 你这是什么意思?
    • 完成希望这会有所帮助!
    • 你不是在使用单独的 JS 文件吗?您需要在单独的渲染文件中调用 js。代码应该这样: Template.postList.rendered({$('#accordion_id') .accordion() ;})
    猜你喜欢
    • 2011-05-01
    • 2014-03-31
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多