iframe框架在ie瀏覽器下將白色背景設(shè)為透明色

字號:


    最近在做項(xiàng)目的過程中需要頻繁的使用iframe畫頁面的層級框架,大部分瀏覽器是沒有背景的,但是iframe在ie中默認(rèn)是有一個白色的背景色的,當(dāng)主背景色不是白色的時(shí)候,在這一部分會顯得很突兀。幫助手冊中給出了解決方案,即將iframe中的allowtransparent屬性設(shè)置為true,即可。在幫助文檔中也給出了相應(yīng)的示例,示例如下:
    代碼如下:
    <body style=background-color: red>
    <iframe id=frame1 src=transparentbody.htm allowtransparency=true>
    </iframe>
    <iframe id=frame2 src=transparentbody.htm allowtransparency=true
    style=background-color: green>
    </iframe>
    <iframe id=frame3 src=transparentbody.htm>
    </iframe>
    <iframe id=frame4 src=transparentbody.htm
    style=background-color: green>
    </iframe>
    </body>
    理論上,這樣做就沒問題了,但是呢ie好像反應(yīng)不大,依舊還是有白色的背景。其實(shí)到了這一步,我們還需要在子頁面的<body>標(biāo)簽添加<body bgcolor=transparent>,示例如下:
    http://img.blog.csdn.net/20140610165850968?watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqva3vucgvuz19tdwjhbw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center
    main.html主頁面代碼:
    代碼如下:
    <html>
    <head>
    <title></title>
    </head>
    <body style=background-color:blue;>
    <table
    style=width:100%;border:0;height:100%;cellpadding:0;cellspacing:0>
    <tr height=100%>
    <td height=100% width=30%>
    <iframe id=test1 src=test1.html allowtransparency=true width=100% height=100%></iframe></td>
    <td height=100% width=40%>
    <iframe id=test2 src=test2.html allowtransparency=true width=100% height=100%></iframe></td>
    <td height=100% width=30%>
    <iframe id=test3 src=test3.html width=100% height=100%></iframe></td>
    </tr>
    </table>
    </body>
    </html>
    test1.html頁面代碼:
    代碼如下:
    <html>
    <head>
    <title></title>
    </head>
    <body bgcolor=transparent>
    <h1>test1</h1>
    </body>
    </html>
    test2.html頁面代碼:
    代碼如下:
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <h1>test2</h1>
    </body>
    </html>
    test3.html頁面代碼:
    代碼如下:
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <h1>test3</h1>
    </body>
    </html>
    這個小例子主要是iframe標(biāo)簽的allowtransparency屬性應(yīng)用,在該屬性設(shè)置為true并且iframe所加載的子頁面的<body>標(biāo)簽背景顏色設(shè)置為transparent(透明)時(shí),iframe將透明化。
    allowtransparency設(shè)置或獲取對象是否可為透明。
    bgcolor 設(shè)置或獲取對象的背景顏色。