如何使用className来实现撤销所有操作的功能?

来源:4-1 编程挑战

HackettHsu

2019-12-18 16:30

<!DOCTYPE HTML>

<html>

<head>

<meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />

<title>javascript</title>

<style type="text/css">

body{font-size:12px;}

#txt{

    height:400px;

    width:600px;

border:#333 solid 1px;

padding:5px;}

p{

line-height:18px;

text-indent:2em;}

.backup{

    height:400px;

    width:600px;

border:#333 solid 1px;

padding:5px;}

</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="changecolor()">  

    <input type="button" value="改变宽高" onClick="changesize()">

    <input type="button" value="隐藏内容" onClick="changehidden()">

    <input type="button" value="显示内容" onClick="changedisplay()">

    <input type="button" value="取消设置" onClick="cancel()">

  </form>

  <script type="text/javascript">

//定义"改变颜色"的函数

function changecolor()

{

    var colorchanger=document.getElementById("txt");

    colorchanger.style.color="red";

    colorchanger.style.backgroundColor="#4169E1";

}

//定义"改变宽高"的函数

function changesize()

{

    var sizechanger=document.getElementById("txt");

    sizechanger.style.width="400px";

    sizechanger.style.height="400px";

}

//定义"隐藏内容"的函数

function changehidden()

{

    var hiddenchanger=document.getElementById("txt");

    hiddenchanger.style.display="none";

}

//定义"显示内容"的函数

function changedisplay()

{

    var displaychanger=document.getElementById("txt");

    displaychanger.style.display="block";

}

//定义"取消设置"的函数

function cancel()

{

    if (confirm("撤销所有修改?"))

    {

        /*var cancelchanger=document.getElementById("txt");

        cancelchanger.style.color="#000";

        cancelchanger.style.backgroundColor="#fff";

        cancelchanger.style.width="600px";

        cancelchanger.style.height="400px";

        cancelchanger.style.display="block";*/

        document.getElementById("txt").className="backup";

    }

}

  </script>

</body>

</html>

<!--我尝试使用这个,但是无法实现目的-->


写回答 关注

5回答

  • 慕的地5313871
    2022-06-11 18:09:20

    索引,遍历你所点的标签

    gfeliqwrfgbmdbbxmecwfbjopmvnk

  • 帝普一世
    2020-02-13 14:33:36

    你这个取消设置的方法也没错,但无法完全达到要求,因为显示与隐藏这2个是冲突的,如何通过改变样式的方法来取消????

    最好的办法是通过removeAttribute来清除所有样式。

    //定义"取消设置"的函数function addqx(){	var addqxa=confirm("真的要取消吗?")	if(addqxa!=true)	{		document.getElementById("con").removeAttribute("style");		document.getElementById("txt").removeAttribute("style");	}}

    但是教程并没有教大家removeAttribute

  • Neil小仙女
    2019-12-29 22:53:34

    这个好

  • 恶龙咆哮啊呜
    2019-12-26 16:09:24

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
    <title>javascript</title>
    <style type="text/css">
    body{font-size:12px;}
    #txt{
        height:400px;
        width:600px;
     border:#333 solid 1px;
     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="colorTxt()"> 
        <input type="button" value="改变宽高" onclick="widthhe()">
        <input type="button" value="隐藏内容" onclick="yc()">
        <input type="button" value="显示内容" onclick="xs()">
        <input type="button" value="取消设置" onclick="exit()">
      </form>
      <script type="text/javascript">
    //定义"改变颜色"的函数
     var con = document.getElementById("con");
     var txt = document.getElementById("txt");
    function colorTxt(){
        con.style.color = "red";
        con.style.bakcgroundColor = "#ccc"
        txt.style.color = "blue";
       
    }
    //定义"改变宽高"的函数
    function widthhe(){
        con.style.width = "100px";
        txt.style.width = "200px";
        con.style.height = "100px";
        txt.style.height = "200px";
    }

    //定义"隐藏内容"的函数
    function yc(){
        con.style.display = "none";
        txt.style.display = "none";
    }

    //定义"显示内容"的函数
    function xs(){
        con.style.display="block";
        txt.style.display="block";
    }

    //定义"取消设置"的函数
    function exit(){
        var x = confirm("请问是否要取消全部的a样式?");
        if(x == true){
            con.style="none";
            txt.style="none";
        }
       
    }


      </script>
    </body>
    </html>


  • qq_慕容0063602
    2019-12-21 15:34:30

    前面有一个id{style:none}

JavaScript入门篇

JavaScript做为一名Web工程师的必备技术,本教程让您快速入门

738639 学习 · 9561 问题

查看课程

相似问题