<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>javascript</title> <style type="text/css"> body { font-size: 12px; } #txt { height: 400px; width: 600px; border: 1px solid #333; padding: 5px; } p { line-height: 18px; text-indent: 2em; } </style> </head> <body> <h2 id="con">JavaScript课程</h2> <div id="txt"> <h5>JavaScript为网页添加动态效果并实现与用户交互的功能。</h5> <p>1. JavaScript入门篇,让不懂JS的你,快速了解JS。</p> <p>2. JavaScript进阶篇,让你掌握JS的基础语法、函数、数组、事件、内置对象、BOM浏览器、DOM操作。</p> <p>3. 学完以上两门基础课后,在深入学习JavaScript的变量作用域、事件、对象、运动、cookie、正则表达式、ajax等课程。</p> </div> <form> <input type="button" value="改变颜色" onclick="color()"> <input type="button" value="改变宽度" onclick="wid_height()"> <input type="button" value="隐藏内容" onclick="none()"> <input type="button" value="显示内容" onclick="block()"> <input type="button" value="取消设置" onclick="quxiao()"> </form> <script type="text/javascript"> function color() { var yanse = document.getElementById("txt"); yanse.style.color = "#fff"; yanse.style.backgroundColor = "#333"; } function wid_height() { var kuangao = document.getElementById("txt"); kuangao.style.width = "400px"; kuangao.style.height = "600px"; } function none() { var yincang = document.getElementById("txt"); yincang.style.display = "none"; } function block() { var xianshi = document.getElementById("txt"); xianshi.style.display = "block"; } function quxiao() { var quxiao = confirm('是否取消设置'); if (quxiao == true) { var quxiao = document.getElementById("txt"); quxiao.style.color = "#000"; quxiao.style.backgroundColor = "#fff"; quxiao.style.width = "600px"; quxiao.style.height = "400px"; quxiao.style.display = "block"; } else { } } </script> </body> </html>
// 变量
var objCon = document.getElementById('con');
var objTxt = document.getElementById('txt');
变量可以定义在函数外面,只需要定义一次所有函数都可以使用
取消设置的函数可以这样写
//定义"取消设置"的函数
function cancelSetUp()
{
let check;
check = confirm();
if(check==true)
{
objCon.removeAttribute("style");
objTxt.removeAttribute("style");
}
}
希望有能帮到你
尽可能避免拼音命名吧,好的代码规范也是同样重要的