本文详细回顾了前端基础知识,包括HTML、CSS和JavaScript的基础内容,并深入解析了几道前端大厂面试真题,帮助读者了解面试中常见的技术问题。此外,文章还提供了实战演练和面试技巧,全面助力读者准备前端大厂面试。
HTML(超文本标记语言)是构建网页的基础语言,定义了网页的结构和内容。HTML文档由元素构成,元素使用标签定义,每个标签通常包含一个开始标签和一个结束标签。
<!DOCTYPE html> <html> <head> <title>示例页面</title> </head> <body> <h1>这是标题</h1> <p>这是段落。</p> </body> </html>
CSS(层叠样式表)用于控制网页的样式和布局,允许我们改变网页的字体、颜色、背景、布局等。
CSS选择器用于选择HTML文档中的特定元素并应用样式。
/* 选择所有p元素 */ p { color: blue; } /* 选择ID为header的元素 */ #header { background-color: gray; } /* 选择class为sidebar的所有元素 */ .sidebar { width: 200px; } /* 选择class为sidebar的所有li元素 */ .sidebar li { color: white; }
JavaScript是一种在浏览器中运行的脚本语言,用于为网页添加交互性。
JavaScript支持多种数据类型,包括数字、字符串、布尔值、数组、对象等。
let num = 123; // 数字类型 let str = "Hello World"; // 字符串类型 let bool = true; // 布尔类型 let arr = [1, 2, 3]; // 数组类型 let obj = { name: "Alice", age: 25 }; // 对象类型
HTML DOM(文档对象模型)是HTML文档的标准模型,通过它,JavaScript可以访问和操作HTML文档中的元素。BOM(浏览器对象模型)提供了与浏览器窗口进行交互的对象和方法。
// 获取元素 const element = document.getElementById('example'); console.log(element); // 设置属性 element.style.color = 'red'; // 添加事件监听器 element.addEventListener('click', () => { console.log('元素被点击'); });
CSS选择器用于选择HTML文档中的元素。常见的选择器有ID选择器、类选择器、元素选择器、伪类选择器等。
// 选择元素并设置样式 const pElement = document.querySelector('p'); pElement.style.color = 'blue'; const headerElement = document.querySelector('#header'); headerElement.style.backgroundColor = 'gray'; const sidebarElements = document.querySelectorAll('.sidebar'); sidebarElements.forEach(element => { element.style.width = '200px'; });
JavaScript中的作用域决定了变量的可见范围。闭包是JavaScript的一个重要特性,当函数可以访问到它自己的变量环境中的变量时,就形成了一个闭包。
function outerFunction(outerVariable) { return function innerFunction(innerVariable) { console.log(`outerVariable: ${outerVariable}`); console.log(`innerVariable: ${innerVariable}`); } } const closure = outerFunction('outside'); closure('inside'); // 输出 "outside" 和 "inside"
JavaScript中的异步编程机制有回调、Promise、async/await等。
// 使用Promise function fetchUserData() { return new Promise((resolve, reject) => { setTimeout(() => { resolve("用户数据已获取"); }, 1000); }); } fetchUserData().then(data => { console.log(data); }); // 使用async/await async function fetchUserDataAsync() { const data = await new Promise((resolve, reject) => { setTimeout(() => { resolve("用户数据已获取"); }, 1000); }); return data; } fetchUserDataAsync().then(data => { console.log(data); });
实现一个简单的轮播图,包含图片切换和指示符。
<!DOCTYPE html> <html> <head> <style> .carousel { position: relative; width: 300px; overflow: hidden; } .carousel-images { display: flex; transition: transform 0.5s ease-in-out; } .carousel-images img { width: 300px; } .carousel-dots { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); } .carousel-dot { width: 10px; height: 10px; border-radius: 50%; background-color: #ccc; margin: 0 5px; cursor: pointer; } .carousel-dot.active { background-color: #000; } </style> </head> <body> <div class="carousel"> <div class="carousel-images" id="carousel-images"> <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="image1.jpg" alt="Image 1"> <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="image2.jpg" alt="Image 2"> <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="image3.jpg" alt="Image 3"> </div> <div class="carousel-dots" id="carousel-dots"> <div class="carousel-dot" data-index="0"></div> <div class="carousel-dot" data-index="1"></div> <div class="carousel-dot" data-index="2"></div> </div> </div> <script> const carouselImages = document.getElementById('carousel-images'); const carouselDots = document.querySelectorAll('.carousel-dot'); let currentIndex = 0; function showImage(index) { const images = document.querySelectorAll('.carousel-images img'); images.forEach((img, i) => { img.style.transform = `translateX(-${index * 300}px)`; }); currentIndex = index; carouselDots.forEach(dot => { dot.classList.remove('active'); }); const activeDot = carouselDots[currentIndex]; if (activeDot) { activeDot.classList.add('active'); } } carouselDots.forEach((dot, index) => { dot.addEventListener('click', () => { showImage(index); }); }); setInterval(() => { showImage((currentIndex + 1) % 3); }, 2000); </script> </body> </html>
实现一个响应式导航栏,当屏幕宽度小于768px时,导航栏应变为汉堡菜单形式。
<!DOCTYPE html> <html> <head> <style> .navbar { display: flex; justify-content: space-between; align-items: center; background-color: #333; } .navbar a { color: white; text-decoration: none; margin: 0 15px; } .navbar .nav-links { display: none; flex-direction: column; } .navbar .nav-links a { margin: 10px 0; } .navbar .burger { display: none; cursor: pointer; } @media screen and (max-width: 768px) { .navbar .nav-links, .navbar .burger { display: block; } .navbar .nav-links { display: none; } .navbar .burger.active ~ .nav-links { display: block; } } </style> </head> <body> <div class="navbar"> <div class="logo">Logo</div> <div class="nav-links"> <a href="#">Home</a> <a href="#">About</a> <a href="#">Contact</a> </div> <div class="burger" onclick="toggleMenu()"> <div class="line"></div> <div class="line"></div> <div class="line"></div> </div> </div> <script> function toggleMenu() { const burger = document.querySelector('.burger'); const navLinks = document.querySelector('.nav-links'); burger.classList.toggle('active'); navLinks.style.display = burger.classList.contains('active') ? 'block' : 'none'; } </script> </body> </html>
<!DOCTYPE html> <html> <head> <title>图片优化示例</title> </head> <body> <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="small-image.jpg" alt="Small Image"> <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="large-image.webp" alt="Large Image" loading="lazy"> </body> </html>
<!DOCTYPE html> <html> <head> <title>代码优化示例</title> </head> <body> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="https://cdn.example.com/main.min.js"></script> </body> </html>
使用CDN(内容分发网络)可以提高网站的加载速度。
<!DOCTYPE html> <html> <head> <title>CDN示例</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="https://cdn.example.com/jquery.min.js"></script> <link rel="stylesheet" href="https://cdn.example.com/bootstrap.min.css"> </head> <body> <script> // 使用CDN中的jQuery $(document).ready(function() { console.log("jQuery is ready!"); }); </script> </body> </html>
<!DOCTYPE html> <html> <head> <title>面试准备示例</title> </head> <body> <script> // 复习项目经验 const reviewProject = () => { console.log("最近的项目经验:"); // 项目一 console.log("项目一: 项目简介、技术栈、遇到的问题及解决方案"); // 项目二 console.log("项目二: 项目简介、技术栈、遇到的问题及解决方案"); }; reviewProject(); </script> </body> </html>
<!DOCTYPE html> <html> <head> <title>面试回答示例</title> </head> <body> <script> // 清晰回答问题 const answerQuestion = () => { console.log("问题: 如何解决异步编程中的回调地狱?"); console.log("回答: 可以使用Promise和async/await来避免回调地狱,改善代码的可读性和维护性。"); }; answerQuestion(); </script> </body> </html>
<!DOCTYPE html> <html> <head> <title>面试跟进示例</title> </head> <body> <script> // 面试后跟进 const followUpInterview = () => { console.log("面试后跟进步骤:"); console.log("- 发送邮件或短信给面试官,询问未解决的问题。"); console.log("- 准备进一步的技术和文化面试。"); }; followUpInterview(); </script> </body> </html>