目录1. JavaScript网页计划案例下面我将提供一个简朴的JavaScript网页计划案例,该案例将实现一个动态的待办事项列表(Todo List)。用户可以在页面上添加新的待办事项,标志它们为已完成,以及删除它们。这个案例将使用HTML来构建页面结构,CSS来美化页面,以及JavaScript来添加动态功能。 1.1 HTML (index.html)[code]<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Todo List</title> <link rel="stylesheet" href="style.css" rel="external nofollow" > </head> <body> <h1>Todo List</h1> <input type="text" id="todoInput" placeholder="Add new todo..."> <button onclick="addTodo()">Add Todo</button> <ul id="todoList"> <!-- Todo items will be added here --> </ul> <script src="script.js"></script> </body> </html>[/code]1.2 CSS (style.css)[code]body { font-family: Arial, sans-serif; margin: 20px; padding: 0; } #todoList { list-style-type: none; padding: 0; } #todoList li { margin: 10px 0; padding: 10px; background-color: #f4f4f4; border: 1px solid #ddd; display: flex; align-items: center; justify-content: space-between; } #todoList li.completed { text-decoration: line-through; opacity: 0.5; } #todoInput { padding: 10px; margin-right: 10px; width: calc(100% - 120px); } button { padding: 10px 20px; cursor: pointer; }[/code]1.3 JavaScript (script.js)[code]function addTodo() { const input = document.getElementById('todoInput'); const list = document.getElementById('todoList'); const itemText = input.value.trim(); if (itemText) { const itemElement = document.createElement('li'); itemElement.textContent = itemText; // Checkbox for marking todo as completed const checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.onclick = function() { itemElement.classList.toggle('completed', this.checked); }; // Button for deleting todo const deleteButton = document.createElement('button'); deleteButton.textContent = 'Delete'; deleteButton.onclick = function() { list.removeChild(itemElement); }; // Append elements to the list item itemElement.appendChild(checkbox); itemElement.appendChild(document.createTextNode('\u00A0')); // Add space itemElement.appendChild(deleteButton); // Append list item to the list list.appendChild(itemElement); // Clear input field input.value = ''; } } // Optionally, add event listener to input field for Enter key press document.getElementById('todoInput').addEventListener('keypress', function(event) { if (event.key === 'Enter') { addTodo(); } });[/code]1.4说明(1)HTML 部门定义了页面的根本结构,包罗一个输入框用于输入待办事项,一个按钮用于添加待办事项,以及一个无序列表用于表现待办事项。 (2)CSS 部门美化了页面,包罗待办事项列表的样式、输入框和按钮的样式。 (3)JavaScript 部门实现了动态功能:
这个案例展示了怎样使用HTML、CSS和JavaScript来创建一个具有根本动态功能的网页应用。 2. JavaScript网页计划案例不同的功能和计划思绪展示除了上述的待办事项列表案例外,另有很多其他雷同的JavaScript网页计划案例,这些案例展示了不同的功能和计划思绪。以下是一些常见的案例及其简要形貌: 2.1 图片画廊(Image Gallery)(1)功能形貌:
(2)代码示例(简化版): [code]<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image Gallery</title> <style> .gallery img { width: 100px; /* 或其他尺寸 */ height: auto; margin: 10px; cursor: pointer; } .modal { display: none; position: fixed; z-index: 1; /* 其他模态框样式 */ } .modal-content { /* 放大图片的样式 */ } </style> </head> <body> <div class="gallery"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <!-- 更多图片 --> </div> <div id="modal" class="modal"> <span class="close">×</span> <img class="modal-content" id="modalImg"> </div> <script> // JavaScript 代码,用于处理点击事件和表现模态框 // ...(省略详细实现) </script> </body> </html>[/code]2.2 简易天气应用(Weather App)(1)功能形貌:
(2)代码示例(简化版,需要替换API密钥): [code]<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Weather App</title> </head> <body> <h1>Weather App</h1> <input type="text" id="cityInput" placeholder="Enter city"> <button id="getWeather">Get Weather</button> <div id="weatherResult"></div> <script> const apiKey = 'YOUR_API_KEY'; // 替换为你的API密钥 document.getElementById('getWeather').onclick = function() { const city = document.getElementById('cityInput').value; fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`) .then(response => response.json()) .then(data => { const temp = data.main.temp; const weatherDescription = data.weather[0].description; document.getElementById('weatherResult').innerHTML = `Temperature: ${temp}°C<br>Description: ${weatherDescription}`; }) .catch(error => { document.getElementById('weatherResult').innerHTML = 'City not found.'; }); }; </script> </body> </html>[/code]2.3 动态表格(Dynamic Table)(1)功能形貌:
(2)代码示例(由于篇幅限定,仅提供概念性形貌):
这些案例涵盖了网页计划的不同方面,从根本的图片展示到实用的天气查询,再到动态的数据处理。它们都是学习JavaScript网页开辟的良好出发点,并可以根据现实需求进行扩展和定制。 到此这篇关于Java Script网页计划案例的文章就介绍到这了,更多相关Java Script网页计划案例内容请搜刮脚本之家以前的文章或继承欣赏下面的相关文章盼望大家以后多多支持脚本之家! 来源:https://www.jb51.net/javascript/326734dgz.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-2 02:46 , Processed in 0.029272 second(s), 19 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.