问题描述
我正在寻找一种将焦点设置到文本字段或文本区域的简单方法.我宁愿不要将Jquery语法与Ember语法混合使用...,也不要为我想设置焦点的每个文本字段或文本区域创建单独的视图.
I am looking for a simple way to set focus into a textfield or textarea. I prefer not to mix Jquery syntax with Ember syntax ... and I prefer not to create seperate views for each textfield or textarea in which I ever want to set the focus.
有什么建议吗?
我的textArea字段很简单:
My textArea field is simply:
{{view Ember.TextArea valueBinding="body" placeholder="body"}}
谢谢马克
推荐答案
将焦点设置为 TextArea
的最直接方法是:
The most straightforward way to set the focus on a TextArea
would be the following:
App.FocusTextArea = Ember.TextArea.extend({
didInsertElement: function() {
this._super(...arguments);
this.$().focus();
}
});
只要您想要这样的视图,就可以像这样使用它:
And the whenever you want such a view you can use it like so:
{{view App.FocusTextArea valueBinding="body" placeholder="body"}}
通过创建自 Ember.TextArea
扩展的自定义 TextArea
视图,您不必每次创建新视图时都在以所需的行为重用自定义视图.
By creating a custom TextArea
view which extends from Ember.TextArea
you are not creating each time a new view, you are reusing the custom view with the desired behavior.
希望有帮助.
这篇关于灰烬:在某个领域设定焦点的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!