html 面试题
1. html 语义化
HTML 语义化是指使用适当的 HTML 元素来描述文档的结构和内容,以便更好地理解和维护网页。语义化的 HTML 代码不仅有助于搜索引擎优化(SEO),还可以提高网页的可访问性(Accessibility)和可维护性。以下是一些常见的 HTML 语义化元素和用法:
标题(Heading): 使用
<h1>
到<h6>
标签表示标题的层级,其中<h1>
用于最高级标题,<h2>
用于次级标题,依此类推。标题标签不仅有助于页面结构,还有助于屏幕阅读器用户理解页面的层次结构。<h1>主标题</h1> <h2>副标题</h2>
段落(Paragraph): 使用
<p>
标签表示段落文本,将文本块分为逻辑段落,提高可读性。<p>这是一个段落。</p>
列表(List): 使用
<ul>
表示无序列表,<ol>
表示有序列表,<li>
表示列表项,以清晰表示项目的顺序和结构。<ul> <li>项目1</li> <li>项目2</li> <li>项目3</li> </ul>
链接(Link): 使用
<a>
标签表示链接,使用href
属性指定链接的目标。<a href="https://www.example.com">访问示例网站</a>
图像(Image): 使用
<img>
标签表示图像,使用alt
属性提供图像的替代文本以增强可访问性。<img src="image.jpg" alt="描述图像的文本" />
表格(Table): 使用
<table>
表示表格,使用<th>
表示表头单元格,使用<tr>
表示表行,使用<td>
表示数据单元格。<table> <tr> <th>姓名</th> <th>年龄</th> </tr> <tr> <td>John</td> <td>30</td> </tr> </table>
表单(Form): 使用
<form>
表示表单,使用各种输入元素如<input>
、<textarea>
和<select>
来收集用户输入。<form action="/submit" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required /> <label for="password">密码:</label> <input type="password" id="password" name="password" required /> <button type="submit">提交</button> </form>
区块(Sectioning)元素: 使用区块元素如
<header>
、<nav>
、<main>
、<article>
、<section>
和<footer>
来划分页面的不同部分,提高文档的结构和语义。<header> <h1>网站标题</h1> <nav> <ul> <li><a href="/">首页</a></li> <li><a href="/about">关于我们</a></li> </ul> </nav> </header> <main> <article> <h2>文章标题</h2> <p>文章内容...</p> </article> <section> <h2>其他部分</h2> <!-- 其他内容 --> </section> </main> <footer> <p>© 2023 示例网站</p> </footer>
HTML 语义化有助于开发者更好地理解和维护代码,同时也为搜索引擎、屏幕阅读器和其他辅助技术提供了更好的理解页面内容的机会,提高了网页的可访问性和可维护性。
2.是否支持 html5
typeof document.createElement("canvas").getContext === "function";
3. 浏览器执行网页渲染整个过程
浏览器执行网页渲染的过程可以大致分为以下步骤:
URL 解析:用户在浏览器地址栏输入网址后,浏览器首先解析该 URL,确定要访问的服务器和路径。
DNS 解析:浏览器将主机名(域名)转换为 IP 地址,以便连接到正确的服务器。如果域名已经在本地 DNS 缓存中,浏览器会直接使用缓存的 IP 地址;否则,它将向 DNS 服务器发送请求以获取 IP 地址。
建立网络连接:浏览器使用 HTTP 协议与服务器建立连接。这通常涉及到三次握手(TCP 握手)来确保连接的建立。
发送请求:浏览器向服务器发送 HTTP 请求,请求页面的 HTML 内容以及其他资源,如样式表、脚本、图像等。
服务器处理请求:服务器接收请求后,根据请求的 URL 和参数来处理请求,可能会查询数据库、生成动态内容,然后生成响应。
接收响应:浏览器接收服务器的响应,这通常包括 HTML 内容和响应头。
HTML 解析:浏览器开始解析 HTML 内容,构建文档对象模型(DOM),并构建渲染树(Render Tree),这是页面的可视化表示。
样式计算:浏览器解析样式表,计算元素的样式,然后将这些样式应用于渲染树中的元素,确定每个元素在屏幕上的位置和外观。
布局(回流):浏览器根据渲染树的内容和样式信息,确定元素在页面上的确切位置和大小。这一步骤也被称为"回流"。
绘制(重绘):浏览器根据计算的布局信息将页面上的元素绘制到屏幕上。这一步骤也被称为"重绘"。
脚本执行:浏览器执行页面中的 JavaScript 代码。脚本可以修改 DOM、触发事件,甚至修改样式,进而影响页面的呈现。
事件处理:浏览器处理用户输入和页面事件,如鼠标点击、键盘输入等。
重复布局和绘制:如果页面发生交互或动态变化,浏览器可能需要多次重复执行布局和绘制步骤,以确保页面的实时更新。
卸载资源:一旦页面渲染完成,浏览器会开始卸载不再需要的资源,如关闭网络连接,释放内存等。
页面加载完成:当所有内容都已渲染和处理完毕,浏览器会触发
onload
事件,表示页面已经加载完成。
这些步骤构成了浏览器执行网页渲染的整个过程,从发送 HTTP 请求到呈现页面,包括解析 HTML、样式计算、布局和绘制等步骤。这是浏览器如何将网页内容呈现给用户的关键过程。
here's the process of rendering a web page in a browser in English:
URL Parsing: The browser begins by parsing the URL, determining the server and path to access.
DNS Resolution: The browser translates the domain name to an IP address, either from local DNS cache or by querying a DNS server.
Establishing Network Connection: Using the HTTP protocol, the browser establishes a connection with the server, typically involving a three-way handshake (TCP handshake) to ensure a connection.
Sending Request: The browser sends an HTTP request to the server, requesting the page's HTML content along with other resources like stylesheets, scripts, images, etc.
Server Processing: The server receives the request, processes it based on the URL and parameters, which may involve database queries, generating dynamic content, and then generates a response.
Receiving Response: The browser receives the server's response, which includes HTML content and response headers.
HTML Parsing: The browser starts parsing the HTML content, building the Document Object Model (DOM), and creating a Render Tree, which is a visual representation of the page.
Style Calculation: The browser parses style sheets, computes element styles, and applies them to the elements in the render tree, determining each element's position and appearance on the screen.
Layout (Reflow): Using the computed layout information, the browser calculates the exact position and size of each element on the page. This step is also known as "reflow."
Paint (Repaint): Based on the computed layout, the browser draws the page's elements on the screen. This step is also known as "repaint."
Script Execution: The browser executes JavaScript code within the page, which can manipulate the DOM, trigger events, and modify styles, affecting the page's rendering.
Event Handling: The browser handles user input and page events, such as mouse clicks, keyboard input, and more.
Repeat Layout and Paint: If the page is interactive or dynamically changing, the browser may repeat the layout and paint steps several times to ensure real-time updates.
Resource Unloading: After rendering is complete, the browser starts unloading resources that are no longer needed, closing network connections, and releasing memory.
Page Load Completion: When all content is rendered and processed, the browser triggers the
onload
event, indicating that the page has fully loaded.
These steps collectively outline the entire process of how a browser renders a web page, from sending an HTTP request to presenting the content to the user.