[英]disable <li> element upon click
I have an events function 'click .category'
that is called whenever the <li class="category">
is clicked on. 我有一个事件函数
'click .category'
,每当'click .category'
<li class="category">
时,都会调用该函数。 It is an element in a dropdown list, specifically one from Bootstrap. 它是下拉列表中的一个元素,特别是Bootstrap中的一个。 I am really stuck on how to disable that element by adding the
'disabled'
class to the <li>
. 我真的对如何通过在
<li>
添加'disabled'
类来禁用该元素感到<li>
。 I know in jQuery you would just execute a simple function like .addClass
. 我知道在jQuery中,您只会执行一个简单的函数,例如
.addClass
。 Is there a way in Meteor/Blaze to accomplish this? 流星/火焰有没有办法做到这一点?
Have you tried something like the below? 您是否尝试过以下类似方法?
Template.myTemplateName.events({
"click .category": function(event, template){
event.preventDefault();
$('.category').addClass('disabled');
}
});
Here is the standard Meteor way of achieving this using a ReactiveVar
. 这是使用
ReactiveVar
实现此目标的标准流星方法。
HTML 的HTML
<template name="category">
<li class="category {{disabled}}">
{{! category HTML}}
</li>
</template>
JS JS
Template.category.onCreated(function(){
this.disabled = new ReactiveVar(false);
});
Template.category.helpers({
disabled: function(){
var disabled = Template.instance().disabled.get();
return disabled ? "disabled" : "";
}
});
Template.category.events({
"click .category": function(event, template){
var disabled = template.disabled.get();
template.disabled.set(!disabled);
}
});
It's always better to let the Meteor templates rendering engine (Blaze) manipulate the DOM for you. 让Meteor模板渲染引擎(Blaze)为您操纵DOM总是更好。 Using template instances lets you easily write reusable components.
使用模板实例可使您轻松编写可重用的组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.