javascript - difference between objects and events.
漂亮男人
漂亮男人 2017-06-12 09:32:07
0
5
604

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <textarea name="" id="ta" cols="100" rows="15">
        <h1 id="time"></h1>
        <script type="text/javascript">            
            //实例化一个Date对象
            var d1 = new Date();
            var res = "";
            var y = d1.getFullYear();
            var m = d1.getMonth() + 1;
            var d = d1.getDate();
            var h = d1.getHours();
            var i = d1.getMinutes();
            var s = d1.getSeconds();
            res = "今天是"+y+"年"+m+"月"+d+"日,"+h+":"+i+":"+s;
            console.log( res );        
            document.getElementById('time').innerHTML = res;
        </script>
    </textarea>
    <br />
    <br />

    <button id="btn">运行代码</button>
    <script type="text/javascript">
        //获取按钮
        var btn = document.getElementById('btn');
        
        //获取textarea
        var ta = document.getElementById('ta');
        //当点击按钮的时候,需要执行的操作
        btn.onclick = function(){
            //新开一个窗口,
            var w1 =  window.open('','mywindow');
            //需要将textarea中的内容写入新窗口
            w1.document.write( ta.value );
        }
    </script>

</body>
</html>
漂亮男人
漂亮男人

reply all(5)
滿天的星座

btn is an object. First of all, you need to understand the composition of JavaScript, which has three parts.
1. ECMAScript describes the syntax and basic objects of the language.
2. Document Object Model (DOM) describes the methods and interfaces for processing web content.
3. Browser Object Model (BOM) describes the methods and interfaces for interacting with the browser.
And your btn is obtained from the syntax of var btn = document.getElementById('btn') ECMAScript
document.getElementById('btn') gets a document object, which is a Dom object, and then you assign it to btn
Then btn is equivalent to this Dom object. As for btn.onclick onclick itself is a method inherent to this Dom object
So you are right to understand that it is a method of this object, although there is no method in js

给我你的怀抱

Btn is of course the object.
Dom object, has its properties and methods.

習慣沉默

Who told you btn is not a target

btn = document.querySelector("body");
typeof btn; //object
曾经蜡笔没有小新

btn is of course an object, and it itself has the onclick method. You can print btn.onclick before binding a callback to it.
This btn.onclick attribute is not defined by us. It already exists. We just use a callback function to assign a value to it. When relevant messages appear in the event queue, the browser (js engine) will execute btn.onclick ();

阿神

Some people above have told you that btn is an object, which is a node in the DOM object of js
You also use the built-in Date object, Event object, window object, and document object of js
var btn = document .getElementById('btn'); What you said is to get the button label in the DOM node in the form of id, which is also a node belonging to the DOM object
You bind a click event to btn through onclick, which is actually equivalent to you A click event object is added to the button node in the DOM object (click belongs to the event event).
Remember one sentence: everything is an object.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!