首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在流星上插入扣子格

在流星上插入扣子格
EN

Stack Overflow用户
提问于 2013-12-16 03:20:28
回答 1查看 164关注 0票数 0

我最近才开始使用流星试图找出如何使事情在它上工作。我已经为此做了几天了。我正在阅读“发现流星”,并试图找出模板是如何工作的。我在这里做错什么了?如何使按钮网格出现?

JS/jQuery:

代码语言:javascript
复制
if (Meteor.isClient) {
  Template.bubbles.grid = function () {

    var el;
    for(var i=1; i<=64; i++){
       return el = document.createElement('div');
        $(el).addClass('button');
        $(el).on('click', function(){
            $(this).addClass('removed');
        });
        $('#container').append(el);
    }
  };

  Template.hello.events({
    'click input' : function () {
      // template data, if any, is available in 'this'
      if (typeof console !== 'undefined')
        console.log("You pressed the button");
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {

  });
}

CSS:

代码语言:javascript
复制
#container {
    width: 440px;
    max-width: 440px;
}
#container > .button {
    display: inline-block;
    width: 50px;
    height: 50px;
    background-image: url('http://placehold.it/50x50');
    margin-right: 5px;
    margin-bottom: 5px;
    opacity: 0.85;
    transition: all 0.07s ease-in-out;
    -moz-transition: all 0.07s ease-in-out;
    -webkit-transition: all 0.07s ease-in-out;
    cursor: pointer;
}
#container > .button:hover {
    opacity: 1;    
}
#container > .button.removed {
    background-image: none;
}

HTML:

代码语言:javascript
复制
<head>
  <title>bubblepopper</title>
</head>

<body>
  {{> hello}}
</body>
<template name ="grid">
  <div id="container"></div>

</template>
<template name="hello">
  <h1>Hello World!</h1>
  {{> grid}}

</template>

当然,对我来说,这是一件全新的挑战,我是否走上了正确的轨道?我需要做什么来使我的网格出现,并且网格也会被更新到流星服务器上?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-16 03:39:13

当您手动创建DOM元素时,您知道您正在做一些错误的事情。这不是做事情的模板方式,也不是流星的方式。您的代码中还有一些常见的javascript错误(“返回el ..?”)

试一试这样的方法(未经测试):

代码语言:javascript
复制
<template name ="grid">
  {{#each buttons}}
    <div class='button'>button {{value}}</div>
  {{/each}}
</template>

和:

代码语言:javascript
复制
Template.grid.buttons = function () {
    var list = [];
    for(var i=1; i<=64; i++){
       list.push({value: i});
    }
    return list;
};

Template.grid.events({
   'click .button': function(ev) {
         $(ev.target).addClass('removed');
   }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20602918

复制
相关文章

相似问题

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