如何在HTML中指定在<iframe>中显示的页面HTML内容?
在本文中,我们需要在一个iframe中显示页面的HTML内容;浏览器窗口被分割成一个单独的页面。我们可以使用<iframe>标签及其srcdoc属性来实现此任务。
HTML <iframe> 标签
HTML中的<iframe>标签指定一个内联框架。此内联框架用于在当前HTML文档中嵌入另一个文档。此标签受所有浏览器支持,例如Google Chrome、Microsoft Edge/Internet Explorer、Firefox、Safari和Opera等。
<iframe>标签的“srcdoc”属性用于指定要在<iframe>中显示的页面的HTML内容。此属性将HTML代码作为值,并指定要在<iframe>中显示的页面的HTML内容。
示例1
在下面的示例中,我们使用了带<iframe>标签的"srcdoc"属性来指定要在HTML的<iframe>中显示的页面(<h2>元素)的HTML内容。
<!DOCTYPE html> <html> <head> <title> How to specify the HTML content of the page to show in the "iframe" in HTML? </title> </head> <body> <h3>How to specify the HTML content of the page to show in the "iframe" in HTML?</h3> <p>We can do it by using <b>srcdoc</b> attribute of iframe in HTML.</p> <iframe srcdoc="<h2>You can find any tutorial on tutorialspoint</h2>"> <p>Browser does not support iframes.</p> </iframe> </body> </html>
正如我们在输出中看到的,文档中的HTML内容(<h2>元素)显示在<iframe>中。
示例2
以下是HTML Iframes的另一个示例,在这个示例中,我们在iframe中创建了一个表格。
<!DOCTYPE html> <html> <head> <title> How to specify the HTML content of the page to show in the "iframe" in HTML? </title> </head> <body> <h3>How to specify the HTML content of the page to show in the "iframe" in HTML?</h3> <p>We can do it by using <b>srcdoc</b> attribute of iframe in HTML.</p> <iframe srcdoc="<table border = 3> <caption> Abbreviations </caption> <tr> <th> HTML </th> <th> CSS </th> </tr> <tr> <td> Hypertext markup language</td> <td> Cascading style sheet </td> </tr> </table>"> <p>Browser does not support iframes.</p> </iframe> </body> </html>
正如我们在输出中看到的,文档中的HTML内容(HTML表格)显示在<iframe>中。
示例3
默认情况下,所有浏览器都会以默认值显示<iframe>。我们可以使用CSS更改<iframe>的样式。
在下面的示例中,
我们通过使用CSS更改其宽度、高度和边框来更改<iframe>的样式。
我们使用了带<iframe>标签的"srcdoc"属性来指定要在HTML的<iframe>中显示的页面(<div>元素)的HTML内容。
<!DOCTYPE html> <html> <head> <title> How to specify the HTML content of the page to show in the "iframe" in HTML? </title> </head> <body> <h3>How to specify the HTML content of the page to show in the "iframe" in HTML?</h3> <p>We can do it by using <b>srcdoc</b> attribute of iframe in HTML.</p> <iframe srcdoc="<div> Hello Disha</div>" width="50%" height="100" style="border:3px solid green;"> <p>Browser does not support iframes.</p> </iframe> </body> </html>
正如我们在输出中看到的,文档中的HTML内容(<div>元素)显示在<iframe>中,并且我们还使用CSS更改了<iframe>的样式。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP