前端開發系列(五)HTML教程(5)

一、與瀏覽者交互,表單標籤

1.1、使用表單標籤,與用戶交互

網站怎樣與用戶進行交互?答案是使用HTML表單(form)。表單是可以把瀏覽者輸入的數據傳送到服務器端,這樣服務器端程序就可以處理表單傳過來的數據。
語法:<form method="傳送方式" action="服務器文件">
1、<form> :<form>標籤是成對出現的,以<form>開始,以</form>結束。
2、 action :瀏覽者輸入的數據被傳送到的地方,比如一個PHP頁面( save.php )。
3、 method : 數據傳送的方式(get / post)。

代碼示例:

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>表單標籤</title>
</head>
	<body>
		<form method="post" action = "save.php">
			  <label for="username">用戶名:</label>
			  <input type="text"  name="username" id="username" value="" />
			  <label for="pass">密碼:</label>
			  <input type="password"  name="pass" id="pass" value="" />    
			  <input type="submit" value="確定"  name="submit" />
			  <input type="reset" value="重置" name="reset" />
		</form>  
	</body>
</html>

運行結果:
在這裏插入圖片描述

1.2、文本輸入框、密碼輸入框

當用戶要在表單中鍵入字母、數字等內容時,就會用到文本輸入框。文本框也可以轉化爲密碼輸入框。
語法:<input type="text/password" name="名稱" value="文本" />
1、type:
    當type = " text " 時,輸入框爲文本輸入框;
    當type = " password " 時, 輸入框爲密碼輸入框。
2、name: 爲文本框命名,以備後臺程序ASP 、PHP使用。
3、value: 爲文本輸入框設置默認值。( 一般起到提示作用 )

代碼示例:

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>文本輸入框、密碼輸入框</title>
</head>
	<body>
		<form  method="post" action="save.php">
			賬戶: 
			<input  type="text"  name="myName" />
			<br>
			密碼: 
			<input  type="password"  name="pass"/>
		</form> 
	</body>
</html>

運行結果:
在這裏插入圖片描述

1.3、文本域,支持多行文本輸入

當用戶需要在表單中輸入大段文字時,需要用到文本輸入域。
語法:<textarea rows="行數" cols="列數">文本</textarea>
1、<textarea>標籤是成對出現的,以開始,以結束。
2、cols : 多行輸入域的列數。
3、rows : 多行輸入域的行數。
4、<textarea></textarea>標籤之間可以輸入默認值。

代碼示例:

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>文本域</title>
</head>
	<body>
		<form action="save.php" method="post" >
			<label>火影忍者:</label>
			<textarea  cols="50"  rows="10">你看見了我的小熊嗎...</textarea>
			<input type="submit" value="確定"  name="submit" />
			<input type="reset" value="重置"  name="reset" />
		</form> 
	</body>
</html>

運行結果:
在這裏插入圖片描述

1.4、使用單選框、複選框,讓用戶選擇

在使用表單設計調查表時,爲了減少用戶的操作,使用選擇框是一個好主意,html 中有兩種選擇框,即單選框複選框,兩者的區別是單選框中的選項用戶只能選擇一項,而複選框中用戶可以任意選擇多項,甚至全選。
語法:<input type="radio/checkbox" value="值" name="名稱" checked="checked"/>
1、type:
    當 type = " radio " 時,控件爲單選框
    當 type = " checkbox " 時,控件爲複選框
2、value: 提交數據到服務器的值(後臺程序PHP使用)
3、name: 爲控件命名,以備後臺程序 ASP、PHP 使用
4、checked: 當設置 checked = " checked " 時,該選項被默認選中

代碼示例:

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>單選框、複選框</title>
</head>
	<body>
		<form action="save.php" method="post" >
			<label>性別:</label>
			<label>男</label>
			<input type="radio" value="1"  name="gender" />
			<label>女</label>
			<input type="radio" value="2"  name="gender" />
		</form>
	</body>
</html>

運行結果:
在這裏插入圖片描述

1.5、使用下拉列表框,節省空間

下拉列表在網頁中也常會用到,它可以有效的節省網頁空間。既可以單選、又可以多選。
語法: <option value="提交值" selected="selected">顯示值</option>
注意: 設置selected="selected"屬性,則該選項就被默認選中

代碼示例:

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>下拉列表框</title>
</head>
<body>
	<form action="save.php" method="post" >
		<label>愛好:</label>
		<select>
		  <option value="看書">看書</option>
		  <option value="旅遊" selected="selected">旅遊</option>
		  <option value="運動">運動</option>
		  <option value="購物">購物</option>
		</select>
	</form>
</body>
</html>

運行結果:
在這裏插入圖片描述

1.6、使用下拉列表框進行多選

下拉列表也可以進行多選操作,在<select>標籤中設置multiple="multiple"屬性,就可以實現多選功能,在 windows 操作系統下,進行多選時按下 Ctrl 鍵同時進行單擊(在 Mac下使用 Command +單擊),可以選擇多個選項。

代碼示例:

<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>使用下拉列表框進行多選</title>
	</head>
<body>
	<form action="save.php" method="post" >
	<label>愛好:</label>
		<select multiple="multiple">
			<option value="看書">看書</option>
			<option value="旅遊">旅遊</option>
			<option value="運動">運動</option>
			<option value="購物">購物</option>
		</select>
	</form>
</body>
</html>

運行結果:
在這裏插入圖片描述

1.7、使用提交按鈕,提交數據

在表單中有兩種按鈕可以使用,分別爲:提交按鈕、重置按鈕。這一小節講解提交按鈕:當用戶需要提交表單信息到服務器時,需要用到提交按鈕
語法: <input type="submit" value="提交">
type: 只有當 type 值設置爲 submit 時,按鈕纔有提交作用
value: 按鈕上顯示的文字

代碼示例:

<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>提交按鈕</title>
</head>
<body>
	<form  method="post" action="save.php">
		<label for="myName">姓名:</label>
		<input type="text" value=" " name="myName " />
		<input type="submit" value="提交" name="submitBtn" />
	</form>
</body>
</html>

運行結果:
在這裏插入圖片描述

1.8、使用重置按鈕,重置表單信息

當用戶需要重置表單信息到初始時的狀態時,比如用戶輸入 “用戶名” 後,發現書寫有誤,可以使用重置按鈕使輸入框恢復到初始狀態。只需要把 type 設置爲 " reset " 就可以。
語法: <input type="reset" value="重置">
type: 只有當 type 值設置爲 reset 時,按鈕纔有重置作用
value: 按鈕上顯示的文字

代碼示例:

<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>重置按鈕</title>
	</head>
	<body>
		<form action="save.php" method="post" >
			<label>愛好:</label>
			<select>
				<option value="看書">看書</option>
				<option value="旅遊" selected="selected">旅遊</option>
				<option value="運動">運動</option>
				<option value="購物">購物</option>
			</select>
			<input type="submit" value="確定"  />
			<input type="reset" value="重置"  />
		</form>
	</body>
</html>

運行結果:
在這裏插入圖片描述

1.9、form表單中的 label 標籤

label 標籤不會向用戶呈現任何特殊效果,它的作用是爲鼠標用戶改進了可用性。如果你在 label 標籤內點擊文本,就會觸發此控件。就是說,當用戶單擊選中該 label 標籤時,瀏覽器就會自動將焦點轉到和標籤相關的表單控件上(就自動選中和該 label 標籤相關連的表單控件上)。
語法: <label for="控件id名稱">
注意: 標籤的 for 屬性中的值應當與相關控件的 id 屬性值一定要相同。

代碼示例:

<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>form中的lable標籤</title>
	</head>

	<body>
		<form>
			<label for="male">男</label>
			<input type="radio" name="gender" id="male" />
			<br />
			<label for="female">女</label>
			<input type="radio" name="gender" id="female" />
			<br />
			<label for="email">輸入你的郵箱地址</label>
			<input type="email" id="email" placeholder="Enter email">
		</form>
	</body>
</html>

運行結果:
在這裏插入圖片描述


此篇博客代碼下載地址:HTML教程5代碼下載
博主的所有博客目錄如下:博客文章目錄彙總
Java面試部分的博客目錄如下:Java筆試面試目錄

轉載請標明出處,原文地址:https://blog.csdn.net/weixin_41835916 如果覺得本文對您有幫助,請點擊支持一下,您的支持是我寫作最大的動力,謝謝。
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章