gpt4 book ai didi

jquery-mobile - 使用 Meteor 和 jQuery Mobile 更改按钮文本/图像

转载 作者:行者123 更新时间:2023-12-01 11:49:14 24 4
gpt4 key购买 nike

我正在使用 Meteor 构建一个应用程序,我有点不清楚如何使用 jQuery Mobile 将所有代码组合在一起。

基本上,我在页眉中有一个编辑按钮,单击时我希望内容部分中的内容发生变化,编辑按钮应更改为保存。单击保存按钮应更新内容并将按钮恢复到原始状态。

编辑按钮看起来像:

<a data-icon="plus" data-role="button" class="edit" >edit</a>

这是一个没有 JS/JQ 的 fiddle :http://jsfiddle.net/AU2cB/3/

想法是在单击编辑按钮时显示输入字段,并在单击保存时显示更新的用户输入文本。我显然还没有接触到服务器部分,所以任何关于如何使用 Meteor 做到这一点的建议都将是一个奖励(我使用 {{> loginButtons }} 进行 facebook 登录)

注意:我对所有人都很陌生。这个的。这是一个相对简单的应用程序,所以在根目录中我只有 1 个 html 文件和一个带有 if (Meteor.is_client) & if (Meteor.is_server) 语句的 javascript 文件。

最佳答案

假设您的按钮位于模板中:

<body>
{{> editButton}}
{{> fields}}
</body>

<template name="editButton">
<a data-icon="plus" data-role="button" class="edit" >edit</a>
</template>

要将其与 Meteor 连接起来,请将事件附加到模板:

Template.editButton.events({
"click [data-role='button']": function(e) {
if ($(e.target).text() == "edit")
$(e.target).text("save");
else
$(e.target).text("edit");
}
});

这将在您单击按钮时切换按钮的文本。那么如何显示或隐藏相关字段呢?我们可以使用 Session :

Template.editButton.events({
"click [data-role='button']": function(e) {
if ($(e.target.text() == "edit") {
$(e.target).text("save");
Session.set("editing", true);
}
else {
$(e.target).text("edit");
Session.set("editing", false);
}
}
});

现在你需要监听 editing 的值,并用它来告诉 Meteor 是否应该显示相关字段:

<template name="fields">
{{#if editing}}
<input type="text" name="myField"/>
{{/if}}
</template>

Template.fields.editing = function() {
return Session.get("editing");
}

现在,当您单击该按钮时,Meteor 将更新关联的 Session 键的值,并且由于 Session 是响应式的,它将重新运行 Template.fields.editing 函数并重新呈现字段模板。

要保存用户输入的数据,您也可以使用 Session。我会让你自己弄清楚那部分,它与我在此处编写的代码非常相似。要持久保存用户信息,请查看 Meteor.user()Collections .

关于jquery-mobile - 使用 Meteor 和 jQuery Mobile 更改按钮文本/图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com