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

Thuộc tính target của Form trong HTML DOM

Thuộc tính target của form chỉ định nơi hiển thị phản hồi sau khi submit: tab mới, tab hiện tại, frame cha, toàn bộ cửa sổ, hoặc frame đặt tên.

Cú pháp

formObject.target = "_blank|_self|_parent|_top|framename"

Giá trị

  • _blank: Mở trong tab/cửa sổ mới.
  • _self: Mở trong cùng tab (mặc định).
  • _parent: Mở trong frame cha.
  • _top: Mở trong toàn bộ thân cửa sổ (thoát tất cả frame).
  • framename: Mở trong frame có tên cụ thể.

Ví dụ

Thay đổi target form động:

<!DOCTYPE html> <html> <head> <style> 
  form { border: 2px solid blue; margin: 2px; padding: 4px; } 
</style> 
<script> 
  function SubmitForm() { 
    document.getElementById("FORM1").submit(); 
  } 
  function ChangeTarg() { 
    document.getElementById("FORM1").target = "_blank"; 
    document.getElementById("Sample").innerHTML = "Target hiện là _blank (mở tab mới)."; 
  } 
</script> </head> 
<body> 
<h1>Ví dụ Form target</h1> 
<form id="FORM1" method="post" action="/sample_page.php"> 
  <label>Tên: <input type="text" name="usrN"></label><br><br> 
  <label>Tuổi: <input type="text" name="Age"></label><br><br> 
  <input type="button" onclick="SubmitForm()" value="SUBMIT"> 
</form> 
<button onclick="ChangeTarg()">THAY ĐỔI TARGET</button> 
<p id="Sample"></p> 
</body> </html>

Kết quả

Mặc định submit trong cùng tab:

Thuộc tính target của Form trong HTML DOM

Sau khi đổi target = "_blank", submit mở tab mới:

Thuộc tính target của Form trong HTML DOM