Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Phương thức writeln() trong HTML DOM


Phương thức writeln() trong HTML DOM cung cấp chức năng ghi nhiều biểu thức (HTML hoặc JavaScript) trực tiếp vào một tài liệu.

LƯU Ý – Phương thức này ghi đè mã HTML trong tài liệu nếu có và thêm các đối số vào dòng mới.

Cú pháp

document.write(agruments)

Ví dụ

Ví dụ minh họa phương thức HTML DOM document writeln():

<!DOCTYPE html> <html> <head> <title>HTML DOM writeln()</title> <style>    * {       padding: 2px;       margin:5px;    }    form {       width:70%;       margin: 0 auto;       text-align: center;    }    input[type="button"] {       border-radius: 10px; </style> </head> <body>    <form>       <fieldset>          <legend>HTML-DOM-writeln( )</legend>          <input id="urlSelect" type="url" placeholder="Nhập URL..."><br>          <input id="textSelect" type="text" placeholder="Họ tên"><br>          <input type="button" value="Đi đến" onclick="openWindow()">          <input type="button" value="Đóng" onclick="closeWindow()">          <input type="button" value="Khôi phục" onclick="restoreWindow()">          <div id="divDisplay"></div>       </fieldset>    </form>    <script>       var urlSelect = document.getElementById("urlSelect");       var textSelect = document.getElementById("textSelect");       var winSource;       function openWindow() {          browseWindow = window.open(urlSelect.value, "browseWindow", "width=400, height=200");          winSource = urlSelect.value;          browseWindow.opener.document.getElementById("divDisplay").textContent = "Cửa sổ          con hoạt động";          if(textSelect.value !== 'admin')             browseWindow.document.writeln("<p><strong>Người dùng không được ủy quyền:             </strong></p>","<p>"+textSelect.value+"</p>");       }       function closeWindow(){          if(browseWindow){             browseWindow.close();             browseWindow.opener.document.getElementById("divDisplay").textContent = "Cửa sổ             con đã đóng";          }       }       function restoreWindow(){          if(browseWindow.closed){             browseWindow = window.open(winSource, "browseWindow", "width=400, height=200");             browseWindow.opener.document.getElementById("divDisplay").textContent = "Cửa sổ             con đã khôi phục";          }       }    </script> </body> </html>

Kết quả

Nhấn nút „Đi đến“ với Họ tên không được ủy quyền:

Phương thức writeln() trong HTML DOM

Nhấn nút „Đi đến“ với Họ tên được ủy quyền:

Phương thức writeln() trong HTML DOM