本文示範如何取得表單的 target 屬性,也就是表單送出後要在哪個視窗或框架中開啟回應頁面。
寫法與讀取其他表單屬性一致:先取得節點,再存取其 target。
<!DOCTYPE html> <html> <body> <form id="frm1" action="form_action.asp" target="_blank"> First name: <input type="text" name="fname" value="Donald"><br> Last name: <input type="text" name="lname" value="Duck"><br> <input type="submit" value="Submit"> </form> <p>The value of the target attribute is: <script> document.write(document.getElementById("frm1").target); </script> </p> </body> </html>
上述程式碼的輸出結果如下:
The value of the target attribute is: _blank
值得留意的是,target 除了 _blank、_self、_parent、_top 這幾個保留值,也可以指定為某個 iframe 的 name,讓表單結果在該框架內呈現。
希望本文所述對大家的 JavaScript 程式設計有所幫助。