本文示範如何讀取 iframe 的 marginHeight 與 marginWidth 屬性,這兩個值代表框架內容區域的上下與左右留白。
<!DOCTYPE html> <html> <body> <iframe id="myframe" src="demo_iframe.htm" marginheight="50" marginwidth="50"> <p>Your browser does not support iframes.</p> </iframe> <br><br> <p>The value of the marginheight attribute and marginwidth are: <script> document.write(document.getElementById("myframe").marginHeight); document.write(document.getElementById("myframe").marginWidth); </script> <p> </body> </html>
使用提醒:這兩個屬性在 HTML5 中已不建議使用,效果等同於對 iframe 內容套用 CSS margin。若需要控制內層留白,比較標準的做法是在 iframe 內部的文件上設定 body { margin: 0 } 或指定的邊距,這樣跨瀏覽器的行為也更一致。
希望本文所述對大家的 JavaScript 程式設計有所幫助。