当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML Base target用法及代码示例


HTML DOM中的基本目标属性用于设置或返回<base>元素的目标属性的值。 target属性用于为页面中的所有超链接和表单指定默认目标。

用法:

  • 它返回基本目标属性。
    baseObject.target
  • 它用于设置基本目标属性。
    baseObject.target = "_blank|_self|_parent|_top|framename"

属性值:


  • _blank:用于在新窗口中打开链接。
  • _self:用于在同一框架中打开链接。
  • _parent:它用于打开父框架集中的链接。
  • _top:它会在整个窗口中打开一个链接。
  • framename:它将在名称dframe中打开一个链接。

返回值:它返回一个字符串值,该值表示页面中所有超链接和表单的默认目标。

范例1:本示例返回基本目标Property。

<!DOCTYPE html>  
<html>  
  
<head>  
    <base id="Geek_Base"
        href="https://www.geeksforgeeks.org" target="_self">  
    <title>  
        HTML | DOM Base target Property  
    </title>  
</head>  
  
<body style="text-align:center;">  
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>  
  
    <h2>HTML | DOM Base target Property</h2>  
  
    <button onclick="myGeeks()">  
        Click  
    </button>  
      
    <h4 id="Geek_p" style="color:green;font-size:24px;"></h4>  
      
    <!-- Script to use DOM Base target Property -->
    <script>  
        function myGeeks() {  
              
            var x = document.getElementById("Geek_Base").target;  
              
            document.getElementById("Geek_p").innerHTML = x;  
        }  
    </script>  
</body>  
  
</html>                    

输出:
在单击按钮之前:

单击按钮后:

范例2:本示例设置基本目标Property。

<!DOCTYPE html>  
<html>  
  
<head>  
    <base id="Geek_Base" href= 
    "https://www.geeksforgeeks.org" target="_self">  
      
    <title>  
        HTML | DOM Base target Property  
    </title>  
</head>  
  
<body style="text-align:center;">  
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>  
  
    <h2>HTML | DOM Base target Property</h2>  
  
    <button onclick="myGeeks()">  
        Click  
    </button>  
      
    <h4 id="Geek_p" style="color:green;font-size:24px;"></h4>  
      
    <!-- Script to use DOM Base target Property -->
    <script>  
        function myGeeks() {  
   
            var x = document.getElementById("Geek_Base").target 
                = "_blank";  
              
            document.getElementById("Geek_p").innerHTML 
                = "The value of the target attribute was " 
                + "changed to " + x;  
        }  
    </script>  
</body>  
  
</html>                    

输出:
在单击按钮之前:

单击按钮后:

支持的浏览器:下面列出了DOM基本目标属性支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • Firefox
  • Opera
  • Safari


相关用法


注:本文由纯净天空筛选整理自ManasChhabra2大神的英文原创作品 HTML | DOM Base target Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。