更改Meteor应用程序文本的多个按钮
Multiple buttons that change the text of a Meteor app
我刚开始使用MeteorJS
如果我有一系列按钮,有没有办法根据我单击的按钮来更改页面的正文?
如果我有一个名为"Hello"的按钮,我希望正文更改为"你好"。
感谢
使用Session
变量
HTML
<body>
{{> main}}
</body>
<template name="main">
<p>{{text}}</p>
{{> button}}
</template>
<template name="button">
<button type="button">Hello</button>
</template>
JS-
Template.main.onCreated(function(){
Session.set("text", "Default");
});
Template.main.helpers({
text: function(){
return Session.get("text");
}
});
Template.button.events({
"click button": function(event, template){
var buttonText = template.$("button").text();
Session.set("text", buttonText);
}
});
使用ReactiveVar
来避免全局性并提供更好的组件封装
HTML也是一样的。
JS-
Template.main.onCreated(function(){
this.text = new ReactiveVar("Default");
});
Template.main.helpers({
text: function(){
return Template.instance().text.get();
}
});
Template.button.events({
"click button": function(event, template){
var buttonText = template.$("button").text();
// search for a field named text in the closest parent template (main)
template.get("text").set(buttonText);
}
});
我们需要添加2个包才能使此代码工作:
meteor add reactive-var
meteor add aldeed:template-extension
相关文章:
- 我想重定向点击,然后更改按钮文本,我该怎么做
- 使用JavaScript将标题文本替换为按钮文本
- 将按钮文本从“;喜欢“;至“;喜欢“;单击时
- 如何在jQuery mobile中更改按钮文本
- 不便更改Javascript中的按钮文本
- 为什么我的导航按钮或按钮文本不会移动到中心
- 根据单选按钮值更改提交按钮文本
- 更改按钮文本
- 按钮文本中的新行
- 在呈现 D3 SVG 对象之前更改引导按钮文本
- 制作一个“;输入“;按钮文本不可编辑
- 通过引导程序加载/重置后更改按钮文本
- 动态创建的按钮文本
- jQuery移动列表过滤:如何更改清除按钮文本
- 如何使用JavaScript在点击时更改按钮文本
- sap.m.按钮文本格式化程序
- AngularJS按钮文本不起作用
- 如何在单击时更改按钮文本
- 使用Jquery仅切换按钮文本中的ASCII字符
- TinyMCE 编辑输入值和按钮文本