如何在没有所有 HTML 的情况下将焦点设置到 HTML 输入元素?
一、背景: 我正在使用 Tapestry 4,因此任何给定页面的 HTML 都是由分散在应用程序中的各种 HTML 片段拼接在一起的。 对于我正在处理的组件,我没有 标记,因此我无法为其指定
onload
属性。
该组件有一个输入元素,在页面加载时需要获得焦点。 有谁知道如何在页面加载时将焦点设置到文件输入(或任何其他文本类型输入)而无需访问正文标记?
我尝试过将脚本插入正文中,例如document.body.setAttribute('onload', 'setFocus()')
(其中 setFocus 是一个将焦点设置到文件输入元素的函数),但这不起作用。 但我不能说我对此感到惊讶。
编辑:
如前所述,我确实需要使用页面组件来执行此操作。 我最终将文件类型输入添加到我们用来将焦点放在页面上第一个可编辑和可见输入的脚本中。 在研究这个问题时,我没有发现这样做有任何安全问题。
First, the background:
I'm working in Tapestry 4, so the HTML for any given page is stitched together from various bits and pieces of HTML scattered throughout the application. For the component I'm working on I don't have the <body>
tag so I can't give it an onload
attribute.
The component has an input element that needs focus when the page loads. Does anyone know a way to set the focus to a file input (or any other text-type input) on page load without access to the body tag?
I've tried inserting script into the body likedocument.body.setAttribute('onload', 'setFocus()')
(where setFocus is a function setting the focus to the file input element), but that didn't work. I can't say I was surprised by that though.
EDIT:
As has been stated, I do indeed need to do this with a page component. I ended up adding file-type inputs to the script we use for giving focus to the first editable and visible input on a page. In researching this problem I haven't found any security issues with doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我想一定有用!
must be useful i think !!!
这对我来说效果很好:
This has worked well for me:
你可以给窗口一个 onload 处理程序
you can give the window an onload handler
我认为你的封装存在根本问题。 尽管在大多数情况下,您可以将事件处理程序附加到 onload 事件 - 请参阅 http://ejohn .org/projects/flexible-javascript-events/,作者:John Resig,了解如何执行此操作,setFocus 需要由页面组件管理,因为页面上不能有两个需要获得焦点的组件当页面加载时。
I think you have a fundamental problem with your encapsulation. Although in most cases you could attach an event handler to the onload event - see http://ejohn.org/projects/flexible-javascript-events/ by John Resig for how to do this, setFocus needs to be managed by a page component since you can't have two components on your page requiring that they get the focus when the page loads.
尝试使用 tabstop属性
Try play with tabstop attribute
首先,输入文件与其他输入不同,您需要记住这一点......这是出于安全原因。 当输入文件获得焦点时,它应该是只读的,或者浏览器应该弹出一个对话框来选择某个文件。
现在,对于其他输入,您可以在某些元素上尝试一些 onload 事件...(不仅主体有 onload 事件),或者您可以在 html 中间使用内联 javascript。 如果您放置 JavaScript 代码而不告诉它是一个函数,那么它会在浏览器读取它时执行。 类似于:
当浏览器读取警报时,该函数将在警报之后执行。
如果可以的话,您应该使用 jQuery 来执行 javascript。 它会让你的生活变得非常轻松......:)
First of all, the input file is no the same as the other inputs, you need to keep this in mind.... thats for security reasons. When the input file get focus it should be read only or the browser should popup a dialog to choose some file.
Now, for the other inputs you could try some onload event on some of your elements...(not only the body have the onload event) or you could use inline javascript in the middle of the html. If you put javascript code without telling that is a function it gets executes while the browser reads it. Something like:
The function will be executed after the alert just when the browser reads it.
If you can, you should use jQuery to do your javascript. It will make your live soooo much easy.... :)
使用 jQuery 可以这样完成:
使用纯 javascript 可以这样完成:
有关使用纯 javascript 的更详细的解决方案,请参阅在 CodeProject 上将焦点设置为网页上的第一个输入。
With jQuery could be done like this:
With plain javascript could be done like this:
For more elaborate solution with plain javascript see Set Focus to First Input on Web Page on CodeProject.
Scunliffe 的解决方案具有可用性优势。
当页面脚本加载缓慢时,如果用户滚动页面,从“onLoad”事件调用 focus() 会导致非常令人讨厌的页面“跳转”。 所以这是一种更加用户友好的方法:
Scunliffe's solution has a usability advantage.
When page scripts are loading slowly, calling focus() from "onLoad" event makes a very nasty page "jump" if user scrolls away the page. So this is a more user friendly approach: