简体   繁体   中英

How to get elements from nested <html> document?

<html><head>
<title>Welcome</title>
<style type="text/css"></style></head>
    <frameset border="false" frameborder="O" framespacing="0" rows="0,*" cols="*">
        <frame marginwidth="0" marginheight="0" name="frame_rsafp" src="rsa_flash_cookie.html" noresize="true" scrolling="no">
        <frameset border="false" frameborder="O" framespacing="0" rows="*,30" cols="*">
            <frame marginwidth="0" marginheight="0" name="login_page" noresize="true" scrolling="yes">
                           #document
                             <html>
                               <form name="frmLogin" action="entry" class="login" method="post" target="_top" autocomplete="off">
                                   <span class="passfield">
                                       <input type="text" name="LoginUserId" onkeypress="return fSubmit(event);" value="" class="ipassword">
                                   </span>
                               </form>
                             </html>                             
            <frame marginwidth="0" marginheight="0" name="footer" src="footer.html" noresize="true" scrolling="no">
        </frameset>
    </frameset>

</html>

For above html code I want to set value to text field " LoginUserId ". I tried document.getElementsByName("LoginUserId")[0].value = "MyValue"; but it returns the error Cannot set property 'value' of undefined . I can only get elements upto " login_page " and count of child elements of " login_page " returns 0 .

var ele = document.getElementsByName("login_page")[0].childNodes;
alert(ele.length);

How can I get elements of this nested html document?

Try this

var iframe = document.getElementsByName("login_page")[0];
var ele    = iframe.contentDocument || iframe.contentWindow.document;

Try this

var loginBox = document.getElementById('login_page');
var values   = loginBox.getElementsByName("LoginUserId")[0].value;

You need to get the iframe first:

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

and then you can work on that document

if you just want to change the value of the LoginUserId input you can do this:

var inputField = document.querySelector("input[name=LoginUserId]");
inputField.value="test";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM