第十章 JavaScript的应用 习题

1数字时钟

HTML代码

html 复制代码
<!DOCTYPE html>
<html>
<head>
	<style type="text/css">
		@import url(./css/1.css);
	</style>
    <meta charset="UTF-8">
    <title>数字时钟</title>

</head>
<body>
	<div class="top">
		<p>数字时钟</p>
	</div>
    <div class="clock" id="clock"><script src="js/1.js"></script>
</div>
    
</body>
</html>

css代码

html 复制代码
body{
    align-items: center;
    height: 100px;
    background-color:#ffffff;
    color: black;
    
}

.clock {
    font-size: 100px;
    border: none;
    padding: 20px;
    border-radius: 10px;
    background-color: rgba(255, 255, 255, 0.1);
	text-align: center;
}
.top,p{
	font-size: 100px;
	text-align: center;
}
html 复制代码
function updateClock() {
    const now = new Date();
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');

    const timeString = `${hours}:${minutes}:${seconds}`;
    document.getElementById('clock').textContent = timeString;
}

// 每秒更新一次时钟
setInterval(updateClock, 1000);

// 初始化时钟
updateClock();

2

html 复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>禁止下载</title>
    <script>
        // 禁用右键菜单并弹出警告
        function showAlert(e) {
            e.preventDefault(); // 阻止默认右键菜单
            alert("禁止下载图片!"); // 弹出警告对话框
        }

        // 添加事件监听器
        document.addEventListener('DOMContentLoaded', function() {
            const images = document.querySelectorAll('img'); // 选择所有图片
            images.forEach(img => {
                img.addEventListener('contextmenu', showAlert); // 为每个图片添加右键事件
            });
        });
    </script>
</head>
<body>
    <img src="img/p1.jpg" alt="示例图片" style="max-width: 100%; height: auto;">
</body>
</html>
相关推荐
小贺要学前端17 分钟前
ES6 还没用明白,JavaScript 已经快到 ES2026 了
前端·javascript·es6
Amumu1213822 分钟前
JS:ES6~ES11基础语法(二)
开发语言·前端·javascript
Amumu1213824 分钟前
Js:ES6~ES11基础语法(一)
开发语言·前端·javascript
weixin_4434785139 分钟前
flutter组件学习之对话框与提示详解
javascript·学习·flutter
Patrick_Wilson2 小时前
你删过 lock 文件吗?聊聊包管理器迁移中 90% 的人会踩的坑
javascript·npm·前端工程化
早點睡3902 小时前
ReactNative项目OpenHarmony三方库集成实战:react-native-permissions
javascript·react native·react.js
氢灵子2 小时前
Fixed 定位的失效问题
前端·javascript·css
英俊潇洒美少年2 小时前
函数组件(Hooks)的 **10 大优点**
开发语言·javascript·react.js
方安乐2 小时前
Javascript工具库:classnames
开发语言·javascript·ecmascript
labixiong2 小时前
React Hooks 闭包陷阱:高级场景与深度思考
前端·javascript·react.js