Control iframe content with javascript/html

前端 未结 2 1826
小鲜肉
小鲜肉 2020-12-06 07:25

Within my webpage I have an iframe like so:


2条回答
  •  日久生厌
    2020-12-06 07:33

    Yes you can. Say your iframe's ID is 'myIFrame'

    
    

    Then, add the following in your JavaScript:

    // Get the iframe
    const iFrame = document.getElementById('myIFrame');
    
    // Let's say that you want to access a button with the ID `'myButton'`,
    // you can access via the following code:
    const buttonInIFrame = iFrame.contentWindow.document.getElementById('myButton');
    
    // If you need to call a function in the iframe, you can call it as follows:
    iFrame.contentWindow.yourFunction();
    

    Hope it helps :)

提交回复
热议问题