如何将超链接从一个 iframe 加载到另一个 iframe?

htmlweb developmentfront end technology更新于 2024/4/16 3:24:17

有时,我们需要点击一个容器中的链接,并在另一个容器中显示内容。在 HTML 中,iframe 用于轻松地在页面的指定区域显示内容。本文使用了两个不同的示例,分别使用链接加载一个 iframe 和另一个 iframe 的链接。在示例 1 中,上层 iframe 中的链接用于在下层 iframe 中显示两张不同的图片。在示例 2 中,使用上层 iframe 中的链接,视频内容会显示在下层 iframe 和上层 iframe 中。

示例 1:使用上层 iframe 中的两个文本链接显示并更改下层 iframe 中的图片内容

文件夹和页面设计步骤 −

步骤 1 − 创建两个文件 iFrameFile1.html 和 iFrameFile2.html。

步骤 2 − 在 iFrameFile1.html 中编写 html 代码,并在该文件中创建两个 iFrame,名称分别为 iframeUpper 和 iframeBottom。

步骤 3 − 保持 iframeBottom 为空,并从 iframeUpper 内部加载 iFrameFile2.html 文件。

步骤 4 − 在 iFrameFile2.html 中编写 html 代码,并将这两个此文件中的 <a> 标签

步骤 5 − 使用 href 以及图片文件的相对路径或绝对路径以及文件名,并在 <a> 标签中使用 target="iframeBottom"

步骤 6 − 在浏览器中打开 iFrameFile1.html。链接将显示在上方的 iframe 中。逐个点击链接,即可查看下方 iframe 中图片文件内容的变化。

本例中使用了以下文件

文件 1 − iFrameFile1.html

文件 2 − iFrameFile2.html

文件 3 − birdalone.jpg

文件 4 − bird.jpg

iFrameFile1.html 的代码

<!DOCTYPE html>
<html>
   <body>
      <center>
         <iframe src=".\iframeFile2.html" 
            name="iframeUpper" 
            width="70%" 
            height="300">
         </iframe>
         <br /><br />
         <iframe src="" 
            name="iframeBottom" 
            width="25%" height="250">
         </iframe>
      </center>
   </body>
</html> 

iFrameFile2.html 代码

<!DOCTYPE html>
<html>
   <body>
      <center>
         <h1 style="color: purple">Showing Beautiful Birds</h1>
         <h2 style="color: cyan">You will love this...</h2>
         <h3 style="color: orange">Just click the links</h2>
         <p>
            <a href=
            "./birdalone.jpg" 
            target="iframeBottom" width="25%" height="250" frameborder="1" allowfullscreen="allowfullscreen">The Cuteee Bird</a>
         </p>
         <p>
            <a href=
            "./bird.jpg" 
             target="iframeBottom" width="25%" height="250" frameborder="1" allowfullscreen="allowfullscreen">The Friends Together</a>
         </p>
      </center>
   </body>
</html> 

查看结果 - 示例 1

要查看结果,请在浏览器中打开 iFrameFile1.html。现在点击链接并检查结果。

示例 2:在上层 iframe 中使用文本链接,在下层 iframe 和上层 iframe 中显示视频内容

文件夹和页面设计步骤 −

步骤 1 − 创建两个文件 iFrameFile11.html 和 iFrameFile22.html。

步骤 2 − 在 iFrameFile11.html 中编写 html 代码,并在该文件中创建两个 iFrame,名称分别为 iframeUpper 和 iframeBottom。

步骤 3 − 保持 iframeBottom 为空,并从 iframeUpper 内部加载 iFrameFile22.html 文件。

步骤 4 − 在 iFrameFile22.html 中编写 html 代码,并将两个 <a>此文件中的标签

步骤 5 − 在两个 <a> 标签中使用 href,其中包含视频文件的相对路径或绝对路径以及文件名,并在一个 <a> 标签中使用 target="iframeBottom",在另一个 <a> 标签中使用 target=_self。

步骤 6 − 在浏览器中打开 iFrameFile11.html。链接将显示在上层 iframe 中。逐个点击链接即可查看视频文件的内容。内容将首先显示在底部 iframe 中,然后显示在同一个上层 iframe 中。

本例中使用了以下文件

文件 1 - iFrameFile11.html

文件 2 - iFrameFile22.html

文件 3 - birdvideo.mp4

iFrameFile11.html 的代码:

<!DOCTYPE html>
<html>
   <body>
      <center>
         <iframe src=".\iframeFile22.html" 
            name="iframeUpper" 
            width="70%" 
            height="300">
         </iframe>
         <br /><br />
         <iframe src="" 
            name="iframeBottom" 
            width="25%" height="250">
         </iframe>
      </center>
   </body>
</html> 

iFrameFile22.html 代码

<!DOCTYPE html>
<html>
   <body>
      <center>
         <h1 style="color: purple">Showing Beautiful Birds Video</h1>
         <h2 style="color: cyan">You will love this...</h2>
         <h3 style="color: orange">Just click the links</h2>
         <p>
            <a href=
            "./birdvideo.mp4" target="iframeBottom">
            First Open the Video in the bottom frame
            </a>
         </p>
         <p>
            <a href=
            "./birdvideo.mp4" target=_self>
            Open The video in the same frame
            </a>
         </p>
   </center>
   </body>
</html> 

查看结果 - 示例 2:

要查看结果,请在浏览器中打开 iFrameFile11.html。现在点击链接并检查结果。

在这篇关于 HTML iframe 和 a-href 的文章中,我们使用了两个不同的示例,通过点击第一个 iframe 中的链接,演示了如何在第二个 iframe 中显示内容。第二个示例也演示了如何在同一个 iframe 中查看内容。第一个示例使用图片文件,而第二个示例使用视频显示示例。


相关文章


有用资源