首页 > 其他 > 详细

Events

时间:2015-06-26 00:09:10      阅读:219      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!