我正在试穿 mail client tutorial在 learn.knockoutjs.com 上。我想在邮件详细信息 block 中添加一个“后退按钮”。但是那个按钮是自动触发的。
我的代码
<div class="viewMail" data-bind="with: chosenMailData">
<button data-bind="click: alert('derp')">derp</button>
<div class="mailInfo">
<h1 data-bind="text: subject"></h1>
<p><label>From</label>: <span data-bind="text: from"></span></p>
<p><label>To</label>: <span data-bind="text: to"></span></p>
<p><label>Date</label>: <span data-bind="text: date"></span></p>
</div>
<p class="message" data-bind="html: messageContent" />
</div>
当我设置chosenMailData viewMail div 时显示。但同时浏览器警告“derp”。我想当我点击按钮时,浏览器会在那时发出警报。怎么了?
最佳答案
警报代码将在 with 绑定(bind)呈现内容时执行。
要么将它包装在一个函数中,要么将它移动到 View 模型中的一个函数中
<button data-bind="click: function() { alert('derp'); }">derp</button>
关于javascript - 在knockoutjs上自动触发点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19276014/