target-name 属性用于指定链接的目标名称,通常用于在同一页面内跳转到指定位置。在实际应用中,可以通过设置 target-name 属性来实现页面内的锚点跳转。
语法
a[target-name="_blank"] {
/* CSS 属性 */
}值
target-name:指定链接的目标名称,通常用于在同一页面内跳转到指定位置。
示例
// www.javascriptcn.com code example
<!DOCTYPE html>
<html lang="en">
<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">
<title>Target Name Example</title>
<style>
a[target-name="_blank"] {
color: blue;
text-decoration: none;
}
</style>
</head>
<body>
<h1>Target Name Example</h1>
<a href="#section1" target-name="_blank">Go to Section 1</a>
<a href="#section2" target-name="_blank">Go to Section 2</a>
<h2 id="section1">Section 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<h2 id="section2">Section 2</h2>
<p>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</body>
</html>在上面的示例中,我们定义了两个带有 target-name="_blank" 属性的链接,点击这些链接将会跳转到页面内指定的位置(Section 1 和 Section 2)。这样可以方便用户在同一页面内快速导航到指定内容。
注意事项
target-name属性仅在 HTML 中的锚点跳转时有效,对于外部链接无效。- 使用
target-name属性时,需要确保目标位置的元素具有对应的 id 属性,以便正确跳转到指定位置。
| 值 | 描述 |
|---|---|
| current | 在链接所在的框架、标签页或窗口中打开超链接。 |
| root | 在当前标签页或窗口中超链接。 |
| parent | 在父框架中打开超链接。如果当前框架没有父框架,则将该值视作 root。 |
| new | target-new |
| modal | 在新的(暂时创建的)模态窗口中打开新窗口。 |
| name | 在已有框架、窗口或标签页中打开链接。如果 name 目的地不存在,则用该名称创建新的目的地。 |