當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


HTML DOM open()用法及代碼示例

DOM open() 方法使用其在 HTML 文檔中的名稱將其參數中指定的節點設置為屬性節點。

用法

以下是語法 -

document.open(type,replace);

這裏type代表文件類型和replace表示新文檔的曆史條目是否從打開此文檔的文檔繼承曆史條目。

示例

讓我們看一個 open() 方法的例子 -

<!DOCTYPE html>
<html>
<head>
<style>
   body{
      text-align:center;
      color:#fff;
      background:#ff7f5094;
   }
   .btn{
      background:#0197F6;
      border:none;
      height:2rem;
      border-radius:2px;
      width:35%;
      margin:2rem auto;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>DOM open() Method Demo</h1>
<button onclick="openMethod()" class="btn">Click me to try Open() Mehtod</button>
<script>
   function openMethod() {
      document.open();
      document.write(`
         <style>
            body{
               text-align:center;
               color:#fff;
               background:#ff7f5094;
            }
         </style>
         <h1>DOM open() Method Demo</h1>
         <p>New document</p>
      `);
      document.close();
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 -

點擊“blue”按鈕使用 open() 方法打開一個新的輸出流。

相關用法


注:本文由純淨天空篩選整理自AmitDiwan大神的英文原創作品 HTML DOM open() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。