为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
JavaScript入门篇 _学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

JavaScript入门篇

慕课官方号 页面重构设计
难度入门
时长 1小时35分
  •     (完整代码,觉得帮到你就给个赞吧)

        实现了任务要求的额外动作:

        1.点击更改颜色或更改宽高con和txt会同时更改样式,也可以同时拥有改变颜色和宽高的样式.

        2.连点同一个按钮(更改颜色或宽高),会根据目前状    态取消样式。而不是在同时拥有改变颜色和宽高两种样式的时候           再点击改变颜色就取消所有样式或无操作.

    <!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;
        }
       
    .WH{
        height:100;
        width:10;
     border:#333 solid 2px;
     padding:1px;
        }
    .color{
        color:red;
        }
       
    .both{
        height:100;
        width:10;
     border:#333 solid 2px;
     padding:1px;
     color:red;
        }

    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(){
        var p1 = document.getElementById("con");
        var p2 = document.getElementById("txt");
        if(p1.className =="WH"){
            p1.className = "both";
            p2.style.color="red";
            p2.style.width="300px";
            p2.style.height="300px";
        }
        else if(p1.className=="both"){
            p1.className = "WH";
            p2.removeAttribute('style');
        }
        else if(p1.className=="color"){
            p1.className = null;
            p2.removeAttribute('style');
        }
        else{
            p1.className = "color";
            p2.style.color="red";
        }
    }

    //定义"改变宽高"的函数
    function changeWH(){
        var p1 = document.getElementById("con");
        var p2 = document.getElementById("txt");
        if(p1.className =="color"){
            p1.className = "both";
            p2.style.color="red";
            p2.style.width="300px";
            p2.style.height="300px";
        }
        else if(p1.className =="both"){
            p1.className = "color";
             p2.removeAttribute('style');
             p2.style.color="red";
        }
        else if(p1.className =="WH"){
            p1.className = null;
            p2.removeAttribute('style');
        }
        else{
            p1.className = "WH";
            p2.style.width="300px";
            p2.style.height="300px";
        }
    }

    //定义"隐藏内容"的函数
    function hide(){
        var p1 = document.getElementById("con");
        p1.style.display="none";
        var p2 = document.getElementById("txt");
        p2.style.display="none";
    }

    //定义"显示内容"的函数
    function show(){
        var p1 = document.getElementById("con");
        p1.style.display="block";
        var p2 = document.getElementById("txt");
        p2.style.display="block";
    }

    //定义"取消设置"的函数
    function cancel(){
        var p1 = document.getElementById("con");
        p1.className = null;
        var p2 = document.getElementById("txt");
        p2.style.display="block";
        p2.removeAttribute('style');
    }


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


    查看全部
    0 采集 收起 来源:编程挑战

    2018-05-25

  • 52e3677900013d6a05020261.jpg

    <script type="text/javascript"> window.open('http://www.imooc.com','_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes')
    </script>


    查看全部
  • 语法:

    prompt(str1, str2);

    参数说明:

    str1: 要显示在消息对话框中的文本,不可修改
    str2:文本框中的内容,可以修改

    返回值:

    1. 点击确定按钮,文本框中的内容将作为函数返回值
    2. 点击取消按钮,将返回null

    看看下面代码:

    var myname=prompt("请输入你的姓名:");
    if(myname!=null)
      {   alert("你好"+myname); }
    else
      {  alert("你好 my friend.");  }


    查看全部
  • <script type="text/javascript">
        var mymessage=confirm("你喜欢JavaScript吗?");
        if(mymessage==true)
        {   document.write("很好,加油!");   }
        else
        {  document.write("JS功能强大,要学习噢!");   }
    </script>


    查看全部
  • 关于JS如何输出空格

    无论在输出的内容中什么位置有多少个空格,显示的结果好像只有一个空格。

    这是因为浏览器显示机制,对手动敲入的空格,将连续多个空格显示成1个空格。

    解决方法:

    1. 使用html标签$nbsp;来解决

    2. 使用CSS样式来解决

    document.write("<span style='white-space:pre;'>"+"  1        2    3    "+"</span>");

    http://www.imooc.com/wiki/view?pid=167

    查看全部
  • 隐藏内容:Object.style.display="none";

    显示内容:Object.style.display="block";

    查看全部
  • <title>函数调用</title>

       <script type="text/javascript">

           function contxt() //定义函数

          {

             alert("哈哈,调用函数了!");

          }

       </script>

    </head>

    <body>

       <form>

          <input type="button"  value="点击我" onclick="contxt()" />  

       </form>

    </body>


    查看全部
  • close()关闭窗口

    关闭本窗口:window.close();

    关闭指定的窗口:<窗口对象>.close();


    查看全部
  • _blank:在新窗口显示目标网页
    _self:在当前窗口显示目标网页
    _top:框架网页中在上部窗口中显示目标网页
    <script type="text/javascript"> window.open('http://www.imooc.com','_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes')
    </script>


    查看全部
  • 改变HTML的样式:Object.style.property=new style;

    比如machar.style.color="red";

    https://img1.sycdn.imooc.com//5b0633cd0001678504840228.jpg

    查看全部
    0 采集 收起 来源:改变 HTML 样式

    2018-05-24

  • 单行注释,在注释内容前加符号 “//”

    多行注释以"/*"开始,以"*/"结束

    查看全部
  • Object.innerHTML,获取或替换HTML元素的内容。其中Object是通过document.getElementById(“id”)所获取到的对象。innerHTML区分大小写

    查看全部
    0 采集 收起 来源:innerHTML 属性

    2018-05-24

  • JC中输出内容分:

    1、直接输出“  ”里面的内容

    <script type="text/javascript">
      document.write("I love JavaScript!"); //内容用""括起来,""里的内容直接输出。</script>

    2、

    查看全部
  • 通过ID获取元素:document.getElementById("id"),

    可以通过访问id得到当中的内容

    查看全部
  • <script type="text/javascript">

           function contxt() //定义函数

          {

             alert("哈哈,调用函数了!");

          }

       </script>

    </head>

    <body>

       <form>

          <input type="button"  value="点击我" onclick="contxt()" />      / *函数调用

       </form>            


    查看全部

举报

0/150
提交
取消
课程须知
该课程是针对新手的一个简单基础的课程,让您快速了解JS,通过一些简单的代码编写体会JS。如果您已经对JS有所了解,可以跳过本课程,学习JS进阶课程,进一步学习JS相应的基础知识。学习本课程,希望您至少具备HTML/CSS基础知识,认识常用的标签。
老师告诉你能学到什么?
1. 理解JavaScript基础语法; 2. 掌握常用语句的使用方法; 3. 学会如何获取DOM元素及进行简单操作。
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!