表格与表单元素
表格元素
表格主要是用来展示数据的
表格元素容器的标签table
其他元素的标签
- thead:表头标签
- tbody:表体标签
表格中的数据部分,如图中一行行的数据就是表体 - th:表头单元格
- tr:行
- td:表体单元格
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- 表格元素的容器是table -->
<table>
<!--表头-->
<thead>
<th>序号</th><!--th代表一个单元格-->
<th>姓名</th>
<th>年龄</th>
</thead>
</table>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<thead>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
</thead>
<!--表体:tbody-->
<tbody>
<tr><!--tr代表一行数据-->
<td>1</td> <!--表体每个单元格用td-->
<td>Ben</td>
<td>18</td>
</tr>
<tr>
<td>2</td>
<td>ljs</td>
<td>22</td>
</tr>
<tr>
<td>3</td>
<td>joy</td>
<td>39</td>
</tr>
</tbody>
</table>
</body>
</html>
- 加个边框
将table改成
<table border="1">
- 合并单元格
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td colspan="3">学生列表</td><!--colspan="3"是合并3列单元格的意思-->
</tr>
<tr><!--table可以不加表头,可以直接tr-->
<td>1</td>
<td>Ben</td>
<td>18</td>
</tr>
<tr>
<td>2</td>
<td>ljs</td>
<td>22</td>
</tr>
<tr>
<td>3</td>
<td>joy</td>
<td>39</td>
</tr>
</table>
</body>
</html>
- 纵向合并单元格
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td colspan="4">学生列表</td>
</tr>
<tr>
<td rowspan="3">1班</td><!--这个单元格占了3行-->
<td>1</td>
<td>Ben</td>
<td>18</td>
</tr>
<tr>
<td>2</td>
<td>ljs</td>
<td>22</td>
</tr>
<tr>
<td>3</td>
<td>joy</td>
<td>39</td>
</tr>
</table>
</body>
</html>
- 练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
tbody{
text-align: center;
}
</style>
</head>
<body>
<table border="1">
<tbody>
<tr>
<td colspan="4"><b>学生列表</b></td>
</tr>
<tr>
<td rowspan="4"><b>1班</b></td>
<td><b>序号</b></td>
<td><b>姓名</b></td>
<td><b>年龄</b></td>
</tr>
<tr>
<td>1</td>
<td>Ben</td>
<td>18</td>
</tr>
<tr>
<td>2</td>
<td>ljs</td>
<td>22</td>
</tr>
<tr>
<td>3</td>
<td>Joy</td>
<td>43</td>
</tr>
</tbody>
</table>
</body>
</html>
表单元素
用来向后台服务器提交数据(现学习阶段只关注前端怎么实现)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<form action="">
<!--控件放在form标签中-->
<input type="text" placeholder="用户名">
<!--input作用是一个文本框,属性placeholder是文本框的注释-->
<input type="text" placeholder="密码">
</form>
</body>
</html>
但输入时密码应该是***样式的,所以要在密码那改下type
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<form action="">
<!--控件放在form标签中-->
<input type="text" placeholder="用户名">
<input type="password" placeholder="密码">
<!--type改成password,输入密码就会显示*这种样式-->
</form>
</body>
</html>
- label
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<form action="">
<label for="">用户名</label>
<input type="text" placeholder="用户名">
<label for="">密码</label>
<input type="password" placeholder="密码">
</form>
</body>
</html>
for里面加上id属性时,当点击前面的文字,光标就会出现在对应的文本框中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<form action="">
<label for="username">用户名</label>
<input id="username" type="text" placeholder="用户名">
<label for="password">密码</label>
<input id="password" type="password" placeholder="密码">
</form>
</body>
</html>
点击用户名,输入的光标I就会跑到用户名旁边的文本框中
- select
下拉选项
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<form action="">
<label for="username">用户名</label>
<input id="username" type="text" placeholder="用户名">
<label for="password">密码</label>
<input id="password" type="password" placeholder="密码">
<select name="" id="">
<!--name和id先不用管,name是向服务器发送数据的一个字段-->
<option value="">男</option>
<option value="">女</option>
</select>
</form>
</body>
</html>
- submit提交按钮
这是个将数据提交给后台的按钮
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<form action="">
<label for="username">用户名</label>
<input id="username" type="text" placeholder="用户名">
<label for="password">密码</label>
<input id="password" type="password" placeholder="密码">
<input type="submit">
</form>
</body>
</html>
- button
这是个普通按钮,具体作用要自己写
<input type="button">
<input type="button" value="按钮">
- radio
小圆圈,单选框,不能取消选中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<form action="">
<label for="username">用户名</label>
<input id="username" type="text" placeholder="用户名">
<label for="password">密码</label>
<input id="password" type="password" placeholder="密码">
<input type="submit" value="登录">
<input name="sex" type="radio">
<!--name="sex"把radio进行分类,sex类的radio就只能选一个,没有分类男女的圆圈都能点-->
<!--name和id属性的区别:id属性的值是区分大小写的,每个id值都应该是唯一的;而name属性不具有是唯一性,它的值可以重复使用-->
<label>男</label>
<input name="sex" type="radio">
<label>女</label>
</form>
</body>
</html>
- checkbox
小矩形,复选框,能取消选中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<form action="">
<label for="username">用户名</label>
<input id="username" type="text" placeholder="用户名">
<label for="password">密码</label>
<input id="password" type="password" placeholder="密码">
<input type="submit" value="登录">
<input type="checkbox">
</form>
</body>
</html>
- 练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>调查问卷</h1>
<div class="form">
<form action="">
<label for="username">用户名</label>
<input id="username" type="text" placeholder="用户名"><br>
<label for="password">密码</label>
<input type="password" id="password" placeholder="密码"><br>
<label>性别</label>
<input type="radio" name="sex">
<label name="sex">男</label>
<input type="radio" name="sex">
<label name="sex">女</label><br>
<label>最喜欢的运动</label>
<select>
<option>足球</option>
<option>篮球</option>
<option>排球</option>
<option>网球</option>
</select><br>
<label>最喜欢的水果</label>
<input type="checkbox">苹果
<input type="checkbox">香蕉
<input type="checkbox">雪梨
<br>
<input type="reset">
<input type="submit">
</form>
</div>
</body>
</html>