HTML 常用的标签介绍

本篇文章将简单介绍一下几个标签以及作者在使用以下几个标签时的遇到的问题。

Iframe 标签

目前用的很少,用来嵌套页面,我们会发现使用 iframe 明显页面变得卡了

<iframe src="http://qq.com" frameborder="0" name="xxx"></iframe>

iframe标签默认高度50,宽度为100,所以是一个可替换标签,宽高是自己决定的,可使用CSS给 iframe 一个属性来改变高度和宽度。宽度可以给100%,但是高度不能给100%,没有用,在这只能给确定的值(在CSS中,宽度和高度的规律是非常不一样的,不能通用)。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    iframe{
      width: 100%;
      height: 400px;
    }
  </style>
</head>
<body>
<iframe src="http://qq.com" frameborder="0"></iframe>
</body>
</html>

接下来可以和 a 标签(指定一个 iframe 来打开页面)一起用,在嵌套页面里打开链接,同时知道如何使用 name 属性。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    iframe{
      width: 100%;
      height: 400px;
    }
  </style>
</head>
<body>
  <iframe name=xxx src="http://qq.com" frameborder="0"></iframe>
  <a target=xxx href="http://qq.com">QQ</a>
  <a target=xxx href="http://baidu.com">百度</a>
</body>
</html>

注意 iframe 默认有一个border,很难看,所以一般默认 frameborder="0"

src 属性可以写(支持)相对路径,比如 <iframe src="./index.html" frameborder="0"></iframe> ,当前目录下的index.html。

a 标签

跳转页面(HTTP GET 请求)

<a href="http://qq.com" target="_blank">blank_QQ</a>
<a href="http://qq.com" target="_self">self_QQ</a>
<a href="http://qq.com" target="_parent">parent_QQ</a>
<a href="http://qq.com" target="_top">top_QQ</a>

target 属性

四个 target 属性我们这里用 iframe 来理解。

新建3个html文件:index.htmlindex2.htmlindex3.html

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    iframe{
      width: 100%;
      height: 400px;
    }
  </style>
</head>
<body>
  <iframe src="./index2.html" frameborder="0"></iframe>
</body>
</html>

index2.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    iframe{
      width: 100%;
      height: 400px;
    }
  </style>
</head>
<body>
  <iframe src="./index3.html" frameborder="0"></iframe>
</body>
</html>

index3.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    iframe{
      width: 100%;
      height: 400px;
    }
  </style>
</head>
<body>
  <a href="http://qq.com" target="_blank">blank_QQ</a>
  <a href="http://qq.com" target="_self">self_QQ</a>
  <a href="http://qq.com" target="_parent">parent_QQ</a>
  <a href="http://qq.com" target="_top">top_QQ</a>
</body>
</html>

download 属性

<a href="http://qq.com" download>下载</a>

意思该链接是用来下载的不是用来显示的,不管是什么文件都把它下载下来。

不加 download 属性也会download 主要有由2个层面决定:

  • 由HTTP响应决定,如果HTTP响应的 content-typeapplication/octet-stream,那么浏览器会以下载的形式接收这个请求
  • 如果响应的不是这个 content-type ,但又想让用户下载,那么需要在 a 标签上用 download 属性强制浏览器下载

href 属性

<a href="qq.com">QQ</a>

当做了一个文件,把 .com 当做了文件后缀

<a href="http://qq.com">QQ</a>

HTTP协议

<a href="https://qq.com">QQ</a>

HTTPS协议

<a href="//qq.com">QQ</a>

a 标签的无协议绝对地址

当前页面的协议,我们写代码不许使用file协议,所以为了预览index.html

  • 上传到git hub,用它的预览功能
  • 使用小工具 http-server
    1. 安装 # npm i -g http-server
    2. 运行 # http-server -c-1 意思是运行 http-server 并且不需要保存

index.html

<a href="xxx.html">QQ</a>

<a href="./xxx.html">QQ</a>

相对路径,/xxx.html,只以目录为参考,与文件无关

<a href="#1111">QQ</a>

锚点,/index.html#1111,锚点是页面内的跳转,不发起请求

<a href="?name=frank">QQ</a>

/index.html?name=frank,发起了一个GET请求

<a href="javascript: alert(1);">QQ</a>

<a href="javascript:;">QQ</a>

javascript伪协议,作用:希望 a 标签点击之后不要跳转,什么都不做的标签

form 标签

跳转页面(把内容提交到服务器,HTTP POST 请求,默认还是GET)

如果 form 表单里面没有提交按钮,就无法提交这个 form, 除非用JS

file 协议不支持POST 请求

  <form action="users" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="提交">
  </form>

HTML只有通过 form 标签来上传内容。

通过Chrome开发者工具查看请求内容:

Content-Type: application/x-www-form-urlencoded

  • 注意通过HTTP,密码是明文的 (除非用HTTPS)
  • 输入 111 和 222,form data(请求内容的第四部分)为 username=111&password=222
  • 输入 你好 和 你好,form data(请求内容的第四部分)为 username=%E4%BD%A0%E5%A5%BD&password=%E4%BD%A0%E5%A5%BD
  • www-form-urlencoded规定所有数字英文字母以及键盘上的特殊字符之外的 都要经过转译,UTF-8

如果使用 method="get"

  <form action="users" method="get">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="提交">
  </form>

通过Chrome开发者工具查看请求内容:

  • usernamepassword 参数不会被写到第四部分里面 (form data),默认会把参数放到查询参数里面。没有办法让GET请求有第四部分。
  • 请求第一行:GET /users?username=111&password=222 HTTP/1.1
  • Query String Parameters:username=111&password=222

如果使用 method="post",参数会被默认放到第四部分(form data),但是POST同样可以把参数放到查询参数里面。

  <form action="users?zzz=3" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="提交">
  </form>
  • Query String Parameters:zzz=3

target 属性

参考 a 标签

input 标签

type

  1. submitbutton

  2. checkbox:复选框

    一般希望点选目标文字的时候能自动勾选(使用 label 标签),示例如下:

    <input type="checkbox" id="xxx"><label for="xxx">点击</label>
    

    label 标签的 for 属性和 input 标签的 id 属性 是一对。

    也可以不使用上述两种属性来使用 label 标签 (不写 id 和 for,过于麻烦),用 label 标签 把 input 标签 包起来 ( input 是 label 的儿子 原来是兄弟),示例如下:

    <label for="xxx">用户名</label><input type="text" name="xxx" id="xxx">
    

    改成

    <label>用户名<input type="text" name="xxx"></label>
    

    要把 input 参数放进查询参数或者第四部分里面,必须给 input 添加 name 属性,示例(GET 请求)如下:

      <form action="users" method="get">
      喜欢的水果
        <label><input type="checkbox" name="fruit" value="orange">桔子</label>
        <label><input type="checkbox" name="fruit" value="banana">香蕉</label>
        <input type="submit" value="提交">
      </form>
    

    请求第一行:GET /users?fruit=orange&fruit=banana HTTP/1.1

    Query String Parameters:fruit=orange&fruit=banana

  1. radio:单选按钮,示例如下:

      <form action="users" method="get">
      爱我
        <label><input type="radio" name="loveme" value="yes">Yes</label>
        <label><input type="radio" name="loveme" value="no">No</label>
        <input type="submit" value="提交">
      </form>
    

    checkbox 不同的是当 给 radio 同一个 name 属性 只有一个 value 能被选中。

  2. password:用来输入密码的,只是让人看不见,实际上还是明文的。

    一个值被遮盖的单行文本字段。使用 maxlength 指定可以输入的值的最大长度 。

  3. 更多的表单 <input> 类型可参考MDN:
    https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input

button 标签

如果一个form里面只有一个button按钮(button 标签而且没有写 type),那么会自动升级为 submit 按钮

  <form action="users" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <button>button</button>
  </form>

select 标签

下拉列表

常见用法:

  <form action="users" method="get">
    <select name="group" multiple>
      <option value="">-</option>
      <option value="1">第一组</option>
      <option value="2">第二组</option>
      <option value="3" disabled>第三组</option>
      <option value="4" selected>第四组</option>
    </select>
    <input type="submit" value="button">
  </form>

textarea 标签

多行文本框,宽高可以自己拉动,容易造成bug,所以一般希望固定大小,可以使用CSS来控制宽高或者使用colsrows(大概估计宽高,不够精确,所以推荐使用CSS)。

  1. 使用 colsrows

    <textarea style="resize:none;" name="爱好" cols="100" rows="10"></textarea>
    
  1. 使用CSS

    <textarea style="resize:none; width: 100px; height: 50px;" name="爱好"></textarea>
    

input 和 button 区别

input是没有子元素的,button可以有子元素(比如 <span>)。

区别:是否为「空标签」

table 标签

示例:

<table border=1>
    <colgroup>
      <col width=100>
      <col bgcolor=red width=200>
      <col width=100>
      <col width=70>
    </colgroup>
    <thead>
      <tr>
        <th>项目</th><th>姓名</th><th>班级</th><th>分数</th>
      </tr>
    </thead>
    <tbody>
       <tr>
        <th></th> <td>小明</td> <td>1</td> <td>94</td>
      </tr>
       <tr>
        <th></th> <td>小红</td> <td>2</td> <td>96</td>
      </tr>
      <tr>
        <th>平均分</th> <td></td> <td></td> <td>95</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <th>总分</th> <td></td> <td></td> <td>190</td>
      </tr>
    </tfoot>
  </table>
  • colgroup 里面的 bgcolor 不推荐使用,基本都使用CSS,功能有限。

  • <thead><tbody><tfoot><colgroup> 四个标签顺序是无关的,都会按照头身脚的顺序在浏览器中展示。同时也可以没有这些标签,如果没有 <tbody> ,浏览器会自动补一个。如果没有 <thead><tfoot>,所以内容都在 <tbody>

  • 语义化:注意 tdth 的区别,td 只表示数据。

  • table 的 border默认是有空隙的, 可以合并,使用CSS

    <head>
      <meta charset="utf-8">
      <title>JS Bin</title>
      <style>
        table{
          border-collapse: collapse;
        }
      </style>
    </head>
    

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,776评论 1 45
  • HTML标签解释大全 一、HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(D...
    米塔塔阅读 3,331评论 1 41
  • iframe a form input select textarea table iframe iframe单独...
    xyyojl阅读 630评论 0 1
  • iframe a form input select textarea table iframe iframe单独...
    前端_学徒阅读 510评论 0 0
  • 1. iframe 标签 iframe 标签主要用途是嵌套页面,目前使用较少,只有一些遗留项目在用。具体可以参考 ...
    大喵chary阅读 474评论 0 0