求解!!‘改变宽度’ 和 ‘取消设置’ 按钮没用~

来源:4-1 编程挑战

村口胡大爷

2019-12-24 20:01

<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='change_color()'>  

    <input type="button" value="改变宽高"  onclick='change_size()'>

    <input type="button" value="隐藏内容"  onclick='hideobj()'>

    <input type="button" value="显示内容"  onclick='showobj()'>

    <input type="button" value="取消设置"  onclick='rese()'>

  </form>

  <script type="text/javascript">

  

  var mychar1=document.getElementById("con");

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

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

function change_color(){

    mychar2.style.color='red';

    mychar1.style.color='blue';

    mychar2.style.backgroundColor='green';

}


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

function change_size(){

    mychar2.style.width='1600';

    mychar2.style.height='800';

}


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

function hideobj(){

    mychar2.style.display="none";

    mychar1.style.display="none";

}


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

function showobj(){

    mychar2.style.display="block";

    mychar1.style.display="block";

}



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

function rese(){

    var message = confirm('确认取消所有设置?');

    if(message==ture){

    mychar2.removeAttribute('style');

    mychar1.removeAttribute('style');

    }

}



  </script>

</body>

</html>



‘改变宽度’ 点了完全没反应,‘取消设置’点了会弹出对话框但是不能初始化改变,来个大神看一下

写回答 关注

5回答

  • weixin_慕婉清6531712
    2020-01-12 22:36:50

    你要获取元素啊,

     var mychar= document.getElementById("id");

        mychar.style.width='300px';

        mychar.style.height='400px';


  • 慕标9317703
    2019-12-28 14:42:15

    宽高加px,

    取消设置没有获取到元素

  • 恶龙咆哮啊呜
    2019-12-26 16:08:44

    <!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>


  • weixin_慕少4244930
    2019-12-26 09:42:14

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

        <input type="button" value="改变宽高" onclick="changeWH()"/>

        <input type="button" value="隐藏内容" onclick="hide()"/>

        <input type="button" value="显示内容" onclick="show()"/>

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

      </form>

      <script type="text/javascript">

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

    function changeColor(){

        let info = document.getElementById("txt")

        info.style.color="white";

        info.style.backgroundColor="black";

    }

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

    function changeWH(){

        let info = document.getElementById("txt")

        info.style.width="200px";

        info.style.height="200px";

    }

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

    function hide(){

        let info = document.getElementById("txt")

        info.style.display="none";

    }

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

    function show(){

        let info = document.getElementById("txt")

        info.style.display="block";

    }

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

    function cancel(){

        let info = document.getElementById("txt")

        info.style="none";

    }



      </script>

    </body>

    </html>


    var是舊的用法,最新的JS是用let來寫
    取消就把他想簡單點就可以了,直接命令ID更改

  • 半生伊
    2019-12-25 09:47:32

    改变宽高试试数字后加个px;

    取消设置试一下通过getElementById。

JavaScript入门篇

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

738661 学习 · 9561 问题

查看课程

相似问题