码迷,mamicode.com
首页 > 其他好文 > 详细

Events

时间:2015-06-26 00:10:56      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

 

To create real interactivity on a website, you need events — these are code structures that listen out for things happening to the browser, and then allow you to run code in response to those things. The most obvious example is the click event, which is fired by the browser when the mouse clicks on something. To demonstrate this, try entering the following into your console, then clicking on the current webpage:

document.querySelector(‘html‘).onclick = function() {
    alert(‘Ouch! Stop poking me!‘);
}
 

  

There are many ways to attach an event to an element. Here we are selecting the HTML element and setting its onclick handler property equal to an anonymous (i.e. nameless) function that contains the code we want to run when the click event occurs.

Note that

document.querySelector(‘html‘).onclick = function() {};

  

 

is equivalent to

var myHTML = document.querySelector(‘html‘);
myHTML.onclick = function() {};

  

 

It‘s just shorter.

Events

标签:

原文地址:http://www.cnblogs.com/hephec/p/4601142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!