前文介绍了初识DOM的相关知识点(感兴趣的朋友欢迎翻阅),本文就向大家分享一些利用对DOM操作解决简单实际应用的过程。

- 当点击开关时,页面的颜色进行相应的改变,模拟开关灯的景象。
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>开关灯练习title>
- <link rel="stylesheet" href="./CSS/reset.css">
- <style>
- /* 灯泡区域外部容器样式设置 */
- .light-wrapper {
- width: 200px;
- height: 250px;
- position: relative;
- margin: 100px auto;
- }
-
- .light {
- width: 100%;
- height: 200px;
- background-color: yellow;
- border-radius: 50%;
- position: absolute;
- z-index: 10;
- }
-
- .base {
- width: 140px;
- height: 80px;
- background-color: silver;
- position: absolute;
- z-index: 5;
- top: 150px;
- left: 15%;
- border-radius: 90%;
- }
-
- /* 按钮样式设置 */
- #btn {
- height: 80px;
- width: 80px;
- margin-left: 47.5%;
- font-size: 22px;
- border-radius: 50%;
- }
- style>
- head>
-
- <body>
-
- <div class="light-wrapper">
-
- <div class="light" id="light">div>
-
- <div class="base" id="base">div>
- div>
-
- <div>
- <button id="btn">OPENbutton>
- div>
-
-
-
- <script>
- // 获取 id 为 btn 的按钮
- var btn = document.getElementById('btn');
-
- // 获取页面元素 body
- var body = document.body;
-
- // 获取元素灯泡
- var light = document.getElementById('light');
-
- // 获取元素灯泡底座
- var base = document.getElementById('base');
-
- console.log(light);
- // 设置标识符
- var flag = true;
- // 设置按钮的点击事件
- btn.onclick = function () {
- flag = !flag;
- if (flag) {
- btn.innerText = 'OPEN';
- body.style.backgroundColor = '#fff';
- light.style.backgroundColor = 'yellow';
- base.style.backgroundColor = 'silver';
- } else {
- btn.innerText = 'CLOSE';
- body.style.backgroundColor = 'black';
- light.style.backgroundColor = 'gray';
- base.style.backgroundColor = 'silver';
- }
-
- }
- script>
-
-
- body>
-
- html>

- 根据当前系统时间给予用户相应的提示信息
- 例如,当前时间为上午时,提示信息为“上午好呀,xxxx”。
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>用户登录title>
- <style>
- /* 提示信息外部容器样式设置 */
- .wrapper {
- width: 310px;
- height: 150px;
- background-color: rgba(203, 229, 232, 0.83);
- margin: 100px auto;
- }
-
- /* 标题样式设置 */
- .word {
- background-color: pink;
- font-size: 20px;
- padding-left: 40px;
- }
-
- /* 当前时间区域样式设置 */
- #time {
- padding-left: 20px;
- }
-
- /* 提示信息区域样式设置 */
- #msg {
- padding-left: 20px;
- font-size: 20px;
- }
- style>
- head>
-
- <body>
-
- <div class="wrapper">
-
- <p class="word">Bonsoir:欢迎你的登录!p>
-
- <p id="time">p>
-
- <div id="msg">div>
- div>
-
-
- <script>
- // 获取显示当前时间元素
- var time = document.getElementById('time');
-
- // 获取提示信息区域元素
- var msg = document.getElementById('msg');
-
- // 获取当前系统时间
- var now = new Date();
-
- // 分别获取时间分量
- var year = now.getFullYear();
- var month = now.getMonth() + 1;
- var date = now.getDate();
- var day = now.getDay();
- var hours = now.getHours();
- var minutes = now.getMinutes();
- var seconds = now.getSeconds();
- var dateNow = year + '-' + month + '-' + date + ' ' + '周' + day + ' ' + hours + ':' + minutes + ':' + seconds;
- time.innerText = '当前时间是: ' + dateNow;
-
- // 根据时间进行提示判断
- if (hours < 12) {
- msg.innerText = '上午好呀,Bonsoir!';
- } else if (hours < 17) {
- msg.innerText = '下午好呀,Bonsoir!';
- } else {
- msg.innerText = '晚上好呀,Bonsoir!';
- }
- script>
- body>
-
- html>

- 初始页面时广告区域应该呈现,当用户点击关闭按钮时,关闭当前广告,反之则打开
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>广告的打开与关闭title>
- <link rel="stylesheet" href="./CSS/reset.css">
- <style>
- /*表单区域外部容器样式设置*/
- form {
- width: 300px;
- height: 350px;
- background-color: rgba(205, 228, 236, 0.934);
- margin: 100px auto 0 auto;
- }
-
- /* 按钮样式设置 */
- button {
- width: 60px;
- height: 40px;
- }
-
- /* 广告区域样式设置 */
- .banner {
- width: 100%;
- height: 200px;
- margin-top: 20px;
- background-color: pink;
- font-weight: bold;
- }
- style>
- head>
-
- <body>
-
- <form action="#">
-
- <button id="btn">关闭button>
-
- <div class="banner">我是一个广告!div>
- form>
-
-
-
- <script>
- // 获取广告区域
- var div = document.getElementsByTagName('div')[0];
- // 获取按钮
- var btn = document.getElementById('btn');
-
- // 设置标识符
- var flag = true;
- // 设置按钮的点击事件
- btn.onclick = function () {
- flag = !flag;
- if (flag) {
- btn.innerText = '关闭';
- div.style.visibility = 'visible';
- } else {
- btn.innerText = '打开';
- div.style.visibility = 'hidden';
- }
- }
- script>
-
- body>
-
- html>

- 当输入框获取焦点时,输入框内预留文字消失
- 失去焦点时,输入框内预留文字出现
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>输入框文字样式设置title>
- <link rel="stylesheet" href="./CSS/reset.css">
- <style>
- /* 输入框外部容器样式设置 */
- .wrapper {
- width: 350px;
- margin: 100px auto 0 auto;
- }
-
- /* 输入框样式设置 */
- #input {
- width: 300px;
- height: 30px;
- outline: none;
- }
- style>
- head>
-
- <body>
-
- <form class="wrapper" action="#">
-
- <input type="text" id="input" placeholder="请输入您的用户名:">
- form>
-
-
- <script>
- // 获取输入框
- var input = document.getElementById('input');
- // 为输入框设置获取焦点与失去焦点事件
- input.onfocus = function () {
- input.placeholder = '';
- }
- input.onblur = function () {
- input.placeholder = '请输入您的用户名:';
- }
-
-
- script>
- body>
-
- html>

- 实现密码框显示状态的切换,初始时密码为不可见状态,同时小眼睛的状态也为打开状态
- 当用户点击输入框后的小眼睛,输入框的内容变为文本可见状态,同时小眼睛的状态也为关闭状态。
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>页面密码框title>
- <link rel="stylesheet" href="./JS/font_3547761_ecdde3pwrko/iconfont.css">
- <link rel="stylesheet" href="./CSS/reset.css">
- <style>
- /* 输入框外部容器样式设置 */
- .wrapper {
- width: 300px;
- position: relative;
- margin: 100px auto 0 auto;
- }
-
- /* 输入框样式设置 */
- #input {
- width: 100%;
- height: 30px;
- outline: none;
- }
-
- /* 小图标样式设置 */
- i {
- position: absolute;
- top: 10px;
- right: 7px;
- cursor: pointer;
- }
- style>
- head>
-
- <body>
-
- <div class="wrapper">
-
- <input type="text" id="input">
-
- <i class="iconfont" id="eyes">i>
- div>
-
-
- <script>
- // 获取小眼睛图标
- var eyes = document.getElementById('eyes');
- // 获取输入框
- var input = document.getElementById('input');
- // 设置标识符
- var flag = true;
- // 为小眼睛设置点击事件
- eyes.onclick = function () {
- flag = !flag;
- if (flag) {
- eyes.innerHTML = '';
- input.type = 'text';
- } else {
- eyes.innerHTML = '';
- input.type = 'password';
- }
- }
- script>
- body>
-
- html>
好了,就此停笔,最后依旧诚挚祝福屏幕前的你健康幸福、平安喜乐。