Java从入门到放弃

JavaScript:自定义对象

2018-12-31  本文已影响0人  Deque

二.自定义对象

1.如何创建自定义对象

1.1直接量

采用直接量方式创建的对象也叫JSON对象

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自定义对象</title>
<script type="text/javascript">
    //1.采用直接量的方式创建对象
    function f1() {
        var student = 
            {"name":"张三","age":23,"work":function(){alert("我学Java");}};
        alert(student.name);
        alert(student.age);
        student.work();
    }
</script>
</head>
<body>
    <input type="button" value="按钮1" onclick="f1();"/>
</body>
</html>

1.2构造器

1)内置构造器(new的函数/首字母大写)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自定义对象</title>
<script type="text/javascript">
    
    //2.采用内置构造器创建对象
    function f2() {
        var teacher = new Object();
        teacher.name = "王老师";
        teacher.age = 18;
        teacher.work = function(){
            alert("我教Java");
        }
        alert(teacher.name);
        alert(teacher.age);
        teacher.work();
    }
</script>
</head>
<body>
    <input type="button" value="按钮2" onclick="f2();"/>
</body>
</html>

2)自定义构造器

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自定义对象</title>
<script type="text/javascript">
    //3.采用自定义构造器创建对象
    function Coder(name,age,work){ //定义一个程序员对象
        this.name = name;
        this.age = age;
        //this指代当前对象,.job是给此对象增加job属性 =work是将参数work赋值给此属性
        this.job = work;
    }
    function f3() {
        var coder = new Coder("李四",25,function(){alert("我写Java");})
        alert(coder.name);
        alert(coder.age);
        coder.job();
    }
</script>
</head>
<body>
    <input type="button" value="按钮3" onclick="f3();"/>
</body>
</html>

2.总结

三.事件

1.事件的概述

1)什么是事件

2)事件的分类

2.事件的定义

1)直接定义事件

2)后绑定事件(*)

3)取消事件

演示:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    //1.直接定义事件
    function f1() {
        console.log("直接定义事件");
    }
    
    //2.后绑定事件
    window.onload = function() {
        var btn = document.getElementById("btn2");
        btn.onclick = function() {
            console.log("后绑定事件");       
        }
    }       
</script>
</head>
<body>
    <input type="button" value="按钮1" onclick="f1();">
    <input id="btn2" type="button" value="按钮2">
</body>
</html>

3.事件对象

1)什么是事件对象

2)如何获取事件对象

直接定义事件

后绑定事件

事件对象演示

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    //1.直接定义事件
    function f1(e) {
        console.log("直接定义事件");
        console.log(e);
    }
    
    //2.后绑定事件
    window.onload = function() {
        var btn = document.getElementById("btn2");
        btn.onclick = function(e) {
            console.log("后绑定事件");
            console.log(e);
        }
    }       
</script>
</head>
<body>
    <input type="button" value="按钮1" onclick="f1(event);">
    <input id="btn2" type="button" value="按钮2">
</body>
</html>

4.事件处理机制

1)冒泡机制

冒泡机制的作用

需要知道事件源

3)事件源

计算器案例 计算器案例效果.png
<!DOCTYPE html>
<html>
  <head>
    <title>计算器</title>
    <meta charset="utf-8" />
    <style type="text/css">
      .panel {
        border: 4px solid #ddd;
        width: 192px;
        margin: 100px auto;
        /*border-radius: 6px;*/
      }
      .panel p, .panel input {
        font-family: "微软雅黑";
        font-size: 20px;
        margin: 4px;
        float: left;
        /*border-radius: 4px;*/
      }
      .panel p {
        width: 122px;
        height: 26px;
        border: 1px solid #ddd;
        padding: 6px;
        overflow: hidden;
      }
      .panel input {
        width: 40px;
        height: 40px;
        border:1px solid #ddd;
      }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var div = document.getElementById("jsq");
            div.onclick = function(e) {
                //获取事件源(input/p/div)
                var obj = e.srcElement||e.target;
                //只处理input
                if(obj.nodeName=="INPUT"){
                    var p = document.getElementById("screen");
                    if(obj.value=="C"){
                        //清空p
                        p.innerHTML="";
                    }else if(obj.value=="="){
                        //计算
                        try{
                            var v = eval("("+p.innerHTML+")");
                            p.innerHTML = v;
                        }catch(ex){
                            p.innerHTML = "Error";
                        }
                    }else{
                        //累加
                        p.innerHTML += obj.value;
                    }
                }
            }
        }
    </script>
  </head>
  <body>
    <div id="jsq" class="panel">
      <div>
        <p id="screen"></p>
        <input type="button" value="C">
        <div style="clear:both"></div>
      </div>
      <div>
        <input type="button" value="7">
        <input type="button" value="8">
        <input type="button" value="9">
        <input type="button" value="/">
        
        <input type="button" value="4">
        <input type="button" value="5">
        <input type="button" value="6">
        <input type="button" value="*">
        
        <input type="button" value="1">
        <input type="button" value="2">
        <input type="button" value="3">
        <input type="button" value="-">
        
        <input type="button" value="0">
        <input type="button" value=".">
        <input type="button" value="=">
        <input type="button" value="+">
        
        <div style="clear:both"></div>
      </div>
    </div>    
  </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读