How can I access iframe elements with Javascript?

前端 未结 2 832
攒了一身酷
攒了一身酷 2020-11-22 07:51

I have a webpage where there is a textarea within a iframe. I need to read the value of this textarea from its child page JavaScript. Presently by using window.parent.

相关标签:
2条回答
  • 2020-11-22 08:06

    Using jQuery you can use contents(). For example:

    var inside = $('#one').contents();
    
    0 讨论(0)
  • 2020-11-22 08:20

    If you have the HTML

    <form name="formname" .... id="form-first">
        <iframe id="one" src="iframe2.html">
        </iframe>
    </form>
    

    and JavaScript

    function iframeRef( frameRef ) {
        return frameRef.contentWindow
            ? frameRef.contentWindow.document
            : frameRef.contentDocument
    }
    
    var inside = iframeRef( document.getElementById('one') )
    

    inside is now a reference to the document, so you can do getElementsByTagName('textarea') and whatever you like, depending on what's inside the iframe src.

    0 讨论(0)
提交回复
热议问题