LinkedText<"/> LinkedText<">

首页  >  文章  >  web前端  >  使用 JavaScript 或 HTML 定位窗口

使用 JavaScript 或 HTML 定位窗口

PHPz
PHPz转载
2023-09-04 18:13:021093浏览

使用 JavaScript 或 HTML 定位窗口

在本文中,我们将学习如何使用 TARGET 属性在新窗口中打开网站 HTML 和 JavaScript

HTML 的 TARGET 属性

使用 锚点的 Target 属性指定链接的名为框架或窗口的开口 标签。 HTML 内容中的结束 标记必须出现在每个开始 标记之后 因为 元素是配对的。虽然锚点的 href(超文本引用)元素 标签还有其他几个选项,它是必需的,因为它包含该网页所在的 https://www.php.cn/link/886ad506e0c115cf590d18ebb6c26561 点击后链接就会消失。

语法

<a href="https://www.php.cn/link/886ad506e0c115cf590d18ebb6c26561" target="_top"> Linked Text </a>

属性值 - href 属性指定的 https://www.php.cn/link/886ad506e0c115cf590d18ebb6c26561 的目标窗口由此引用 属性。它可能具有以下任何值 -

  • _top - 它替换任何现有框架,以便使用全文将网页加载到浏览器窗口中。

  • _self - 默认情况下,_self 是目标属性的默认值。它会在打开链接的同一窗口或框架中打开网页。

  • _blank - 网页加载后会打开一个新的浏览器窗口。

  • _parent - 此方法在父窗口或框架集中加载网页。

HTML 的 元素(将显示链接网站的内容)必须具有 如果您希望将网页内容加载到其他框架中,则提供 NAME 属性。 此外,必须指定 元素的 target 属性以及该元素的名称。 将显示其内容的框架。

示例

在这个例子中,让我们了解一下 target="blank" 的用法,如下所示。网页将会打开 每当用户单击链接文本时,都会在新窗口中显示。

<!DOCTYPE html>
<html>
<title>Target a Window Using JavaScript or HTML - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <h2 style="color:rgb(6, 92, 157);">The webpage will launch in a new window after clicking the link below.</h2>
   <a href="https://www.php.cn/link/1a09a1f048354ae7570bf137f30abd21" target="_blank">Welcome to Tutorialspoint website!</a>
</body>
</html>

使用 JavaScript,在新选项卡中打开 https://www.php.cn/link/886ad506e0c115cf590d18ebb6c26561

HTML 中的锚标记是在新选项卡中打开 https://www.php.cn/link/886ad506e0c115cf590d18ebb6c26561 的简单而直接的方法。本节有 有关此标签的更多信息。然而,有些情况下必须使用Javascript来实现 同一个任务。 window.open() 方法在这种情况下很有用。基于浏览器 配置和参数值,window.open()方法可用于打开一个新窗口 浏览器窗口或新选项卡。

策略

  • 我们必须在第二个参数中使用 _blank 才能使用 window.open() 方法打开新选项卡。

  • window.open() 返回的值。 window.open() 返回的引用要么是新生成的窗口或选项卡,要么是 null(如果失败)。

  • 避免向其中添加第三个参数,因为这样做会导致打开新窗口而不是选项卡。

语法

window.open(https://www.php.cn/link/886ad506e0c115cf590d18ebb6c26561, '_blank');

示例

在此示例中,让我们了解如何使用 JavaScript 在新选项卡中打开链接 (https://www.php.cn/link/886ad506e0c115cf590d18ebb6c26561) window.open() 方法。

<!DOCTYPE html>
<html>
<title>Target a Window Using JavaScript or HTML - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body style="text-align:center; padding-top: 50px;">
   <p> Once you click the button <strong>tutorialspoint.com</strong> link will launch in a new tab
   </p><br>
   <button button type="button" class="btn btn-success" onclick="openNewTab()">
      <strong>Open TutorialsPoint</strong>
   </button>
   <script>
      function openNewTab() {
         window.open(
            "https://www.php.cn/link/1a09a1f048354ae7570bf137f30abd21", "_blank");
      }
   </script>
</body>
</html>

示例

<!DOCTYPE html>
<html>
<title>Target a Window Using JavaScript or HTML - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body style="text-align:center; padding-top: 50px;">
   <p> Click the button to learn <strong>JavaScript</strong> with
      <strong>tutorialspoint.com</strong> link, it will launch in a new tab</p><br>
   <button button type="button" class="btn btn-success" onclick="myNewTab()">
      <strong>Open TutorialsPoint</strong>
   </button>
   <script>
      function myNewTab() {
         window.open(
            "https://www.php.cn/link/1a09a1f048354ae7570bf137f30abd21javascript/index.htm", "_blank");
      }
   </script>
</body>
</html>

简介

在 HTML 中,您必须在锚点的 href 属性中包含目标页面的 https://www.php.cn/link/886ad506e0c115cf590d18ebb6c26561 如果您想将用户引导至另一个网站,请使用该元素。如果您希望链接在新窗口中打开 浏览器窗口中,您还必须包含目标。

您可以使用 window.open() 方法在 JavaScript 中完成相同的任务。甚至 虽然我们也可以使用 HTML 来完成此操作,但 JavaScript 选项是有益的。

以上是使用 JavaScript 或 HTML 定位窗口的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除