讲内容显示在iframe中
时间: 2023-07-31 12:11:36 浏览: 58
要在 `iframe` 中显示内容,可以使用 `src` 属性来指定要显示的页面的 URL,也可以使用 JavaScript 动态加载内容。
以下是使用 `src` 属性将页面加载到 `iframe` 中的示例代码:
```html
<iframe id="myFrame" src="https://example.com"></iframe>
```
在上面的代码中,`src` 属性指定了要在 `iframe` 中显示的页面的 URL。当页面加载时,`iframe` 将显示该页面的内容。
如果您想在 JavaScript 中动态加载内容并将其显示在 `iframe` 中,则可以使用 `iframe` 的 `contentWindow` 属性来获取 `iframe` 的 `window` 对象,并使用 `document` 属性获取 `iframe` 的 `document` 对象。然后,您可以使用 `document.write()` 方法或其他 DOM 操作方法将内容添加到 `iframe` 中。以下是一个示例代码:
```html
<iframe id="myFrame"></iframe>
<script>
var iframe = document.getElementById("myFrame");
var win = iframe.contentWindow || iframe;
win.document.write("<h1>Hello World!</h1>");
</script>
```
在上面的代码中,我们获取了 `iframe` 元素的引用,并使用 `contentWindow` 属性获取了 `iframe` 的 `window` 对象。然后,我们使用 `document.write()` 方法在 `iframe` 中写入了一个标题。