在 iframe 的形式中获取文本框

Get textbox within a form of an iframe

本文关键字:获取 取文本 iframe      更新时间:2023-09-26

如何从javascript获取表单中的文本框,并且该表单在iframe中?简化的代码是:

<!-- other code --> 
    <div id="Login_Panel_Div">
        <iframe id="popupLoginScreen" src="login.jsp" name="popupContent">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            </head>
            <body>
                <form id="loginForm" method="post" name="loginForm">
                    <h2>Login to your account</h2>                      
                    <label>Username:</label>
                    <input id="username" type="text" name="j_username">
                    <label>Password:</label>
                    <input id="password" type="password" name="j_password">
                    <button id="submitLoginBtn" value="Login" name="submitLoginBtn" type="submit">Login</button>
                </form>
            </body>
            </html>
        </iframe>
    </div>
<!-- other code -->

如果我想获取 id 为"txtbox"的输入,我该怎么办?

谢谢和问候。

正如肖啸指出的那样,解决方案是:

var iframe = document.getElementById('popupLoginScreen');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var usernameTextBox = innerDoc.getElementById('username');
usernameTextBox.focus();

请求线程关闭。

谢谢。