【发布时间】:2023-03-24 14:47:01
【问题描述】:
我有一个使用语义 UI 的流星应用程序。我为此使用了语义:ui-css 包。
我有一个名为 project 的模板,我想在其中显示一个模式。 我在那里创建了第二个模板,它是这样的模态:
<template name="xform">
<div id="modal" class="ui modal">
<i class="close icon"></i>
<div class="header">New Project</div>
<div class="content">
<div class="ui form" id="newProject">
<div class="one fields">
<div class="field">
<legend>Project Details</legend>
<label>Name</label>
<input id="name" name="name" type="text" placeholder="Project name">
</div>
<input type="submit" class="button small createProject" style="color:white;position:relative;float:right;padding: 0.875rem 1.75rem 0.9375rem 1.75rem;font-size: 0.8125rem;" value="Create">
</div>
</div>
</div>
<div class="actions">
<div class="ui button">
Cancel
</div>
<div class="ui button">
Okay
</div>
</div>
</div>
</template>
然后我已经向这样的按钮添加了一个事件来显示模式:
Template.projects.events({
'click .newProject': function(event, template){
template.$('.ui.modal').modal({
onDeny : function(){
return false;
}}).modal('show');
}
});
如果我第一次单击该按钮,模态将按需要打开。 我可以看到模态现在不在我的模板的 html 中,而是在我的正文的末尾。 我可以使用关闭按钮关闭模式,但是当我现在尝试再次打开它时,它不再打开。 模态 div 仍然在我的身体底部。
我已经尝试了几种方法来解决这个问题,但都没有奏效。
这是我的完整代码:http://i.imgur.com/r9JNrlr
【问题讨论】:
-
您基本上已经回答了这个问题,语义 ui 模态代码将 div 移动到正文的底部,因此当您调用
template.$('.ui.modal')...时,您只是在内部“寻找”.ui.modal模板 - 在本例中为“项目”模板。最简单但不一定正确的方法是只使用全局 jQuery 对象$('.ui.modal')。不一定正确,我的意思是您可能应该使用唯一的 id 属性来引用它,以避免多个模式之间的冲突。 -
我已经按照您刚才描述的方式进行了尝试,仅使用 jQuery 而不引用模板,但随后我得到“未捕获的 TypeError:$(...).modal 不是函数”。我不知道为什么会这样。
-
奇怪,我刚刚敲了一些测试代码,它对我来说工作正常。您的任何模板中是否有 标签?想知道这是否与此处解决的问题类似:stackoverflow.com/a/30381769/2723753
-
不,我正在使用 IronRouter 并加载一个 layoutTemplate,它有标题并在产量中显示内容。我没有身体标签,因为 Iron Router 为我做了这个。有问题吗?
标签: javascript jquery css meteor semantic-ui