One article explains HTML tags and attributes in detail (a brief analysis of the main structure)

青灯夜游
Release: 2022-08-02 19:15:52
Original
2450 people have browsed it

This article will take you through HTML tags and attributes, and talk about the main structure and related tags of HTML documents. I hope it will be helpful to you!

One article explains HTML tags and attributes in detail (a brief analysis of the main structure)

The main structure of HTML

The basic structure of the HTML page is as follows, which includes various A variety of tags required to create web pages (such as doctype, html, head and body, etc.).

         Hello  
这是我的第一个HTML页面
Copy after login
  • Top declaration

    • ##The declaration is the first component of the document , located at the very top of the document.

    • This tag tells the browser the HTML specification used.

  • ##Starts with
  • , ends with, and contains the head tag in the middle And the body tag

The syntax format of HTML tagGenerally, an HTML tag consists of a start tag, attributes , content and end tag. The name of the tag is not case-sensitive, but the values of most attributes need to be case-sensitive, as shown below:

属性 ↓ 
PHP中文网
↑ ↑ ↑ 开始标签 内容 结束标签
Copy after login

In addition to the class attribute, the start tag can also contain other attributes. Information, such as id, title, etc., we will explain these later.

Note that although HTML tags are not case-sensitive in syntax, for the sake of standardization and professionalism, it is strongly recommended to always use lowercase when defining tags.

When using a browser to open the HTML document we wrote, the browser will read the content in the document from top to bottom, and render the content in the tag in the browser based on the HTML tags and attributes. in the vessel.

An HTML document must have some basic tags so that the browser can distinguish between ordinary text and HTML documents. You can use any number of tags depending on the effect you want to achieve, but there are a few things to note:

    All HTML tags must be placed within angle brackets < >;
  • Different tags in HTML can achieve different effects;
  • If a certain tag is used, it must be ended with the corresponding closing tag (Except for autism and labels).
Self-closing and tags

Some HTML tags do not have a separate closing tag, but add / in the opening tag to close it. Such labels are called autistic and labelling. Please see the following example:

C语言中文网Logo  

Copy after login

The closing tag does not need to surround the content, so there is no need for a separate closing tag. Only a small number of HTML tags are self-closing.

Represents HTML comments, used to explain HTML code. The browser will ignore the comment content, so users cannot see the comments on the web page

The concept and use of HTML attributes

What are attributes

Attributes can be provided for HTML tags Some additional information, or modifications to HTML tags. Attributes need to be added in the start tag, and the syntax format is:

attr="value"
Copy after login

attr

represents the attribute name,valuerepresents the attribute value. Attribute values must be surrounded by double quotes""or single quotes''.

Note that although both double quotes and single quotes can surround attribute values, for the sake of standardization and professionalism, please use double quotes as much as possible.

A tag can have no attributes, or it can have one or more attributes.

Examples of using HTML attributes:

欢迎 Tom 来到PHP中文网。

Copy after login

Special properties

There are many HTML attributes, which can be roughly divided into two categories:

Some attributes apply to most or all HTML tags, we call these attributes universal attributes;

Some attributes only apply to one or a few specific HTML tags, we call these attributes special attributes.

TheOne article explains HTML tags and attributes in detail (a brief analysis of the main structure)tag in HTML has two special attributes, src and alt. Thetag also has two special attributes, href and target, as shown in the following example:

Description of the code:

Custom attributes

In addition to its own attributes, HTML also allows us to customize attributes. Although these attributes can be recognized by the browser, But it will not add any special effects. We need to use CSS and JavaScript to process custom attributes and add specified styles or behaviors to HTML tags. The

data-*

attribute is used to store custom data applied behind a private page. It is a new attribute in HTML5.

<element data-*="somevalue">
Copy after login

    somevalue: Specify the attribute value (a string)

data-*属性可以在所有的 HTML 元素中嵌入数据。

自定义的数据可以让页面拥有更好的交互体验(不需要使用 Ajax 或去服务端查询数据)。

data-*属性由以下两部分组成:

  • 属性名不要包含大写字母,在 data- 后必须至少有一个字符。

  • 属性值,该属性值可以是任何字符串

注意: 自定义属性前缀 "data-" 会被客户端忽略。

利用dataset可以获取data-属性构造的对象,该方法目前只能在Chrome 、Opera等部分浏览器中实现,其他浏览器如需获取其属性值需要使用getAttribute和setAttribute来操作。

只要在标签里面以”data-”为前缀定义我们的自定义属性就可以用来进行一些数据的存放。

在标签里设置H5新增的自定义属性
Copy after login
Copy after login

这个data属性还可以应用在CSS中,前提是你的浏览器支持after伪类,以及content的attr属性(低版本的IE不支持):

data属性应用于CSS中
Copy after login
#myDiv{ position: ralative; } #myDiv:hover:after{ position: absolute; top: 0px; left: 0px; content: attr(data-attribute); color: red; }
Copy after login

如何获取data属性的值?

在标签里设置H5新增的自定义属性
Copy after login
Copy after login

1、使用getAttribute来获取

var myDiv = document.getElementById("myDiv"); var theValue = myDiv.getAttribute("user-defined-attribute");
Copy after login

2、使用Html5自定义属性对象Dataset来获取

var myDiv = document.getElementById("myDiv"); var theValue = myDiv.dataset.attribute;
Copy after login

注意:带连字符连接的名称在使用的时候需要命名驼峰化,即大小写组合书写,这与应用元素的style对象类似,dom.style.borderColor。例如data属性为data-other-attribute,则我们要获取相应的值可以使用:myp.dataset.otherAttribute

如果Html元素定义了多个自定义属性,如何获取?

在标签里设置多个自定义属性
Copy after login

1、使用循环遍历

var myDiv = document.getElementById("myDiv"); var attrs = myDiv.attributes, var expense = {}, i, j; for (i = 0, j = attrs.length; i < j; i++) { if(attrs[i].name.substring(0, 5) == 'data-') { expense[attrs[i].name.substring(5)] = attrs[i].value; } }
Copy after login

2、使用dataset属性

var expense = document.getElementById('myDiv').dataset;
Copy after login

注:dataset并不是典型意义上的JavaScript对象,而是个DOMStringMap对象DOMStringMap是HTML5一种新的含有多个名-值对的交互变量

1)、让所有的自定义的属性值塞到一个数组中

var chartInput = []; for (var item in expense) { chartInput.push(expense[item]); }
Copy after login

2)、删掉一个data属性

delete myDiv.dataset.attribute;
Copy after login

3、增加一个data属性

myDiv.dataset.attribute4 = 'value4';
Copy after login

dataset的兼容性处理

如果浏览器不支持dataset,有必要做一下兼容处理:

if(myDiv.dataset) { myDiv.dataset.attribute = "valueXX"; // 设置自定义属性 var theValue = myDiv.dataset.attribute; // 获取自定义属性 } else { myDiv.setAttribute("data-attribute", "valueXX"); // 设置自定义属性 var theValue = myDiv.getAttribute("data-attribute"); // 获取自定义属性 }
Copy after login

结语:

使用dataset操作data要比使用getAttribute速度稍微慢些,虽然使用dataset不能提高代码的性能,但是对于简洁代码,提高代码的可读性和可维护性是很有帮助的。

通用属性介绍

HTML 标签中有一些通用的属性,如 id、title、class、style 等,这些通用属性可以在大多数 HTML 标签中使用,下面来简单介绍一下它们的用法。

1) id

id 属性用来赋予某个标签唯一的名称(标识符),当我们使用 CSS 或者 JavaScript 来操作这个标签时,就可以通过 id 属性来找到这个标签。

为标签定义 id 属性可以给我们提供很多便利,比如:

如果标签中带有 id 属性作为唯一标识符,通过 id 属性可以很方便的定位到该标签;

如果 HTML 文档中包含多个同名的标签,利用 id 属性的唯一性,可以很方便的区分它们。

注意:在一个 HTML 文档中 id 属性的值必须是唯一的。

示例代码如下所示:

 
PHP中文网

//m.sbmmt.com/

Copy after login

2) class

与 id 属性类似,class 属性也可以为标签定义名称(标识符),不同的是 class 属性在整个 HTML 文档中不必是唯一的,我们可以为多个标签定义相同的 class 属性值。另外,还可以为一个 HTML 标签定义多个 class 属性值,如下所示:

PHP中文网

//m.sbmmt.com/
Copy after login

当使用 CSS 或者 JavaScript 来操作 HTML 标签时,同样可以使用 class 属性来找到对应的 HTML 标签。由于 class 属性不是唯一的,所以通过 CSS 或 JavaScript 对 HTML 标签的操作会应用于所有具有同名 class 属性的标签中。

3) title

title 属性用来对标签内容进行描述说明,当鼠标移动到该标签上方时会显示出 title 属性的值,如下例所示:

    演示文档 
HTML教程
Copy after login

运行结果如下图所示:

One article explains HTML tags and attributes in detail (a brief analysis of the main structure)

将鼠标在链接处悬停片刻才能看到提示框。

4) style

使用 style 属性我们可以在 HTML 标签内部为标签定义 CSS 样式,例如设置文本的颜色、字体等,如下例所示:

    演示文档 

//m.sbmmt.com/

PHP中文网LOGO
PHP中文网
Copy after login

运行结果如下图所示:

One article explains HTML tags and attributes in detail (a brief analysis of the main structure)

标签中常用的标签

1、标签</strong></p>

标签用来定义 HTML 文档的标题,只有包含标签的文档才算是一个有效的 HTML 文档。另外,一个 HTML 文档中仅允许存在一个标签,并且标签必须放置在标签中。

2、标签

标签用于为页面中所有相对链接指定一个基本链接,当您设置了基本链接后,当前页面中的所有相对链接都会使用这个基本链接作为前缀,如下例所示:

3、标签

标签经常用于引用外部 CSS 样式表,标签中包含两个主要的属性,分别是 rel 和 href。rel 属性用来指示引用文件的类型,href 属性用来设置外部文件的路径。示例代码如下:

Copy after login

4、

Copy after login

5、 标签

标签用于提供有关 HTML 文档的元数据,例如页面有效期、页面作者、关键字列表、页面描述等信息。 标签定义的数据并不会显示在页面上,但却会被浏览器解析。

6、

Copy after login

7、

当用户的浏览器不支持 JavaScript 脚本或者禁用 JavaScript 脚本时,可以在

欢迎来到PHP中文网

Copy after login

One article explains HTML tags and attributes in detail (a brief analysis of the main structure)

注释可以出现在 HTML 文档的任意位置,包括文档开头、文档末尾、文档中间、标签外部、标签内容中等。

相关推荐:《html视频教程

The above is the detailed content of One article explains HTML tags and attributes in detail (a brief analysis of the main structure). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!