淘汰赛 - 获得点击的元素 [英] Knockout - Getting clicked element
问题描述
我有以下加价:
<fieldset>
<div>
<label class="editor-label">Question 1?</label>
<input type="text" class="editor-field" />
<button type="button" data-bind="click: helpClicked">Help</button>
<p class="help">Help 3</p>
</div>
<div>
<label class="editor-label">Question 2?</label>
<input type="text" class="editor-field" />
<button type="button" data-bind="click: helpClicked">Help</button>
<p class="help">Help 3</p>
</div>
<div>
<label class="editor-label">Question 3?</label>
<input type="text" class="editor-field" />
<button type="button" data-bind="click: helpClicked">Help</button>
<p class="help">Help 3</p>
</div>
</fieldset>
我想切换< p>的可见性
在同一 Div
中使用 help
作为单击按钮。我试图使用$(this)来确定点击了哪个按钮然后我可以从那里获得正确的帮助元素。
I want to toggle the visibility of the the <p>
with the class help
in the same Div
as the clicked button. I am trying to use $(this) to determine which button was clicked and then I could get the correct "help" element from there.
问题在于 $(this)
没有返回点击的按钮。
The problem is that $(this)
isn't returning the clicked button.
目前我试图简单地隐藏点击的按钮喜欢:
At the moment I am trying to simply hide the clicked button like:
var viewModel = {
helpClicked: function () {
$(this).hide();
}
};
ko.applyBindings(viewModel);
这不起作用。有人可以帮忙吗?
This doesn't work. Can anyone help please?
推荐答案
这是一个有一个可能解决方案的jsFiddle:
Here is a jsFiddle with one possible solution:
http://jsfiddle.net/unklefolk/399MF/1/
您可以通过以下语法定位所需的DOM元素:
You can target the DOM elements you want via this syntax:
var viewModel = {
helpClicked: function (item, event) {
$(event.target).hide();
$(event.target).next(".help").show()
}
};
ko.applyBindings(viewModel);
这篇关于淘汰赛 - 获得点击的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!