如何使用 Lift(Scala 框架)将焦点集中在文本框上?
请原谅我的问题极其简单,但我对 Lift(以及 Scala)的世界还很陌生。
我正在按照 Lift 网站上的“入门”教程进行操作:http://liftweb.net/getting_started
我已经启动并运行,但我想对应用程序进行快速修改,以便每次我在文本框中按 Enter 时,它都能保持焦点。我已经设法使用 FocusOnLoad 让它专注于页面加载,但我不太清楚如何让它保持焦点(仅使用 Lift 的类,没有自定义 JavaScript)。
这是我的 def render 方法(绑定部分)代码的样子:
def render =
bind("chat", // the namespace for binding
"line" -> lines _, // bind the function lines
"input" -> FocusOnLoad(SHtml.text("", s => ChatServer ! s)) )
这样可以让它专注于页面加载。但由于这是 Comet 应用程序,因此页面仅加载一次。
我的所有其他代码看起来与教程 FWIW 完全相同。
Pardon my excruciatingly simple question, but I'm quite new to the world of Lift (and Scala, for that matter).
I'm following the "Getting Started" tutorial on the Lift website: http://liftweb.net/getting_started
I've got it up and running but I'd like to make a quick modification to the app so that every time I press enter in the textbox, it maintains focus. I've managed to get it to focus on page load using FocusOnLoad but I can't quite figure out how to get it to keep focus (using only Lift's classes, no custom JavaScript).
Here's what my def render method (the bind part) code looks like:
def render =
bind("chat", // the namespace for binding
"line" -> lines _, // bind the function lines
"input" -> FocusOnLoad(SHtml.text("", s => ChatServer ! s)) )
So that works for getting it to focus upon page load. But since this is a Comet app, the page only loads once.
All my other code looks exactly like the tutorial FWIW.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CometActor 中的
render
方法仅在 CometActor 首次初始化时才会被调用,这会在用户第一次进入聊天页面时发生。之后,页面更新通常发生在lowPriority
或highPriority
方法内部。因此,如果您希望文本框在用户向 CometActor 发送 AJAX 更新后获得焦点,您应该将其添加到这些方法之一。使用 JQuery 的一个例子是这样的:我还没有尝试编译它,所以它可能需要一些轻微的调整。本质上,它只是向浏览器发送另一个 JavaScript 命令,该命令使用 JQuery 查找页面上的文本输入,然后将焦点设置在该控件上。如果有多个文本输入,则需要修改 HTML 模板上要设置焦点的控件的类,然后确保将渲染方法更改为:
-%>< /code> 方法指示 Lift 在绑定阶段不要忽略模板中的任何属性。然后,您可以修改 JQuery 选择器以使用该类来查找要关注的正确控件。 Lift 中的部分表单安全性会隐藏分配给表单及其控件的 ID,以防止 XSS 攻击,因此通常最好使用类选择器来使用 JQuery 或其他一些 Javascript 框架查找表单控件。
The
render
method in a CometActor only gets called when the CometActor is first initialized, which happens when the user first goes to the Chat page. Afterwards, the page updates usually happen inside of thelowPriority
orhighPriority
methods. So if you want the text box to become focused after the user sends an AJAX update to the CometActor, you should add it to one of those methods. An example which uses JQuery would be this:I haven't tried compiling this, so it might need some slight tweaking. Essentially, it's just sending another JavaScript command to the browser that uses JQuery to find a text input on the page and then sets the focus on that control. If there are multiple text inputs, you'll need to modify the class on the HTML template for the control you want to set focus on, then make sure that you change your render method to be:
The
-%>
method instructs Lift to not ignore any attributes in the template during the bind phase. Then you can modify the JQuery selector to use that class to find the right control to focus on. Part of the form security in Lift obscures the ID assigned to forms and their controls to prevent XSS attacks, so it's generally better to use class selectors to find form controls using JQuery or some other Javascript framework.