如何在没有所有 HTML 的情况下将焦点设置到 HTML 输入元素?

发布于 2024-07-17 16:25:56 字数 513 浏览 6 评论 0原文

一、背景: 我正在使用 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 like

document.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

西瑶 2024-07-24 16:25:56
<script>
window.onload = function()  {
     document.getElementById('search_query').select();
    //document.getElementById('search_query').value = ''; 
    // where 'search_query' will be the id of the input element
};
</script>

我想一定有用!

<script>
window.onload = function()  {
     document.getElementById('search_query').select();
    //document.getElementById('search_query').value = ''; 
    // where 'search_query' will be the id of the input element
};
</script>

must be useful i think !!!

云胡 2024-07-24 16:25:56

这对我来说效果很好:

<script>
  function getLastFormElem(){
    var fID = document.forms.length -1;
    var f = document.forms[fID];
    var eID = f.elements.length -1;
    return f.elements[eID];
  }
</script>


<input name="whatever" id="maybesetmaybenot" type="text"/>
<!-- any other code except more form tags -->

<script>getLastFormElem().focus();</script>

This has worked well for me:

<script>
  function getLastFormElem(){
    var fID = document.forms.length -1;
    var f = document.forms[fID];
    var eID = f.elements.length -1;
    return f.elements[eID];
  }
</script>


<input name="whatever" id="maybesetmaybenot" type="text"/>
<!-- any other code except more form tags -->

<script>getLastFormElem().focus();</script>
姐不稀罕 2024-07-24 16:25:56

你可以给窗口一个 onload 处理程序

window.onload = setFocus;

you can give the window an onload handler

window.onload = setFocus;
伏妖词 2024-07-24 16:25:56

我认为你的封装存在根本问题。 尽管在大多数情况下,您可以将事件处理程序附加到 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.

梦醒时光 2024-07-24 16:25:56

尝试使用 tabstop属性

Try play with tabstop attribute

临走之时 2024-07-24 16:25:56

首先,输入文件与其他输入不同,您需要记住这一点......这是出于安全原因。 当输入文件获得焦点时,它应该是只读的,或者浏览器应该弹出一个对话框来选择某个文件。

现在,对于其他输入,您可以在某些元素上尝试一些 onload 事件...(不仅主体有 onload 事件),或者您可以在 html 中间使用内联 javascript。 如果您放置 JavaScript 代码而不告诉它是一个函数,那么它会在浏览器读取它时执行。 类似于:

<script type="text/javascript">

function yourFunction()
{
   ...;
};

alert('hello world!");

yourFunction();

</script>

当浏览器读取警报时,该函数将在警报之后执行。

如果可以的话,您应该使用 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:

<script type="text/javascript">

function yourFunction()
{
   ...;
};

alert('hello world!");

yourFunction();

</script>

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.... :)

匿名的好友 2024-07-24 16:25:56

使用 jQuery 可以这样完成:

$(function() {
    $("input:file").eq(0).focus()
})

使用纯 javascript 可以这样完成:

var oldWindowOnload = window.onload; // be nice with other uses of onload 
window.onload = function() {
    var form = document.forms[0];
    for(i=0; i < form.length; i++) {
        if (form[i].type == "file") {
            form[i].focus();
        }
    }
    oldWindowOnload();
}

有关使用纯 javascript 的更详细的解决方案,请参阅在 CodeProject 上将焦点设置为网页上的第一个输入

With jQuery could be done like this:

$(function() {
    $("input:file").eq(0).focus()
})

With plain javascript could be done like this:

var oldWindowOnload = window.onload; // be nice with other uses of onload 
window.onload = function() {
    var form = document.forms[0];
    for(i=0; i < form.length; i++) {
        if (form[i].type == "file") {
            form[i].focus();
        }
    }
    oldWindowOnload();
}

For more elaborate solution with plain javascript see Set Focus to First Input on Web Page on CodeProject.

关于从前 2024-07-24 16:25:56

Scunliffe 的解决方案具有可用性优势。

当页面脚本加载缓慢时,如果用户滚动页面,从“onLoad”事件调用 focus() 会导致非常令人讨厌的页面“跳转”。 所以这是一种更加用户友好的方法:

<input id="..."></input>
... really small piece of HTML ...
<script>getTheDesiredInput().focus();</script>

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:

<input id="..."></input>
... really small piece of HTML ...
<script>getTheDesiredInput().focus();</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文