• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

learn-co-curriculum/phase-0-javascript-events-event-listening-lab

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

learn-co-curriculum/phase-0-javascript-events-event-listening-lab

开源软件地址:

https://github.com/learn-co-curriculum/phase-0-javascript-events-event-listening-lab

开源编程语言:

JavaScript 53.1%

开源软件介绍:

JavaScript Event Listeners Lab

Learning Goals

  • Create event listeners on DOM nodes using addEventListener()

Introduction

In this lab we will learn how to teach nodes to "listen" for an event using addEventListener().

If you haven't already, fork and clone this lab into your local environment. Navigate into its directory in the terminal, then run code . to open the files in Visual Studio Code.

Create Event Listeners on DOM Nodes with addEventListener()

In order for JavaScript to handle an event, we first need to tell it to listen for that event. We do this by calling the addEventListener() method on the element we want to add the listener to, and passing it two arguments:

  1. the name of the event to listen for, and
  2. a callback function to "handle" the event

Open up index.html in the browser. When you click in the <input> area, nothing happens. Let's set up some event handling. Specifically, let's add an event listener for the click event on the input#button element in index.html.

Try out the following in the Chrome DevTools console:

const input = document.getElementById('button');
input.addEventListener('click', function() {
  alert('I was clicked!');
});

Now when you click inside of input#button, you will get an alert box.

Let's review what's happening in this code.

First, we grab the element that we want to add the event listener to and save a reference to it in the input variable.

Next, we call addEventListener() on that element to tell JavaScript to listen for the event. We pass two arguments to addEventListener(): the name of the event to listen for (in this case, click) and a callback function that will be executed when the event is "heard."

According to MDN:

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

That's exactly what's happening here: we're passing a callback function as the second argument to the addEventListener() function; the callback will be invoked as soon as the event occurs.

Let's pull out that second argument and take a look at it:

function() {
  alert('I was clicked!');
}

This function has all the components of functions we've seen before (the function keyword, a pair of parentheses, and the body of the function enclosed in curly braces) except one: it doesn't have a name assigned to it. This is what's called an anonymous function. Because it doesn't have a name, it can't be invoked directly. But the event listener knows to execute whatever function is passed as the second argument when it detects the event, so it doesn't need to be named.

If we are only calling our callback function in that one place, using an anonymous function makes sense. However, what if we wanted to use that same alert message on a bunch of elements? In that case, it would make more sense to create a separate, named function that could be called by all of our event listeners. With this approach, we would pass the function name as the second argument to addEventListener() rather than the function itself:

const input = document.getElementById('button');

function clickAlert() {
  alert('I was clicked!');
}

input.addEventListener('click', clickAlert);

We could then attach our clickAlert to as many elements as we'd like. Just as we did for the input element, we would first use our CSS selector skills to grab the desired element and save it to a variable, then add the click event listener to that element. Give it a try!

With this approach, even if we're using our clickAlert with a whole bunch of elements, if we decide later that we want to change the text of the alert to "Hee hee, that tickles!" instead, we would only need to make that change in one place: inside our clickAlert() function.

Note: we pass clickAlert as the argument, not clickAlert(). This is because we don't want to invoke the function in this line of code. Instead, we want to pass a reference to the function to addEventListener() so it can call the function when the time comes.

Refresh your browser and try out the latest version of the code in the console to verify that it works. Also try passing clickAlert() as the second argument rather than clickAlert and see what happens.

Passing the Tests

Now let's set up index.js to do the same thing so we can get our test passing. To do that, simply copy the code into the index.js file's addingEventListener() function and run the test. Either version should pass the test — just make sure that the code creating the event listener is inside the addingEventListener() function.

Checking the Code in the Browser

We know that the code works in the console and passes the test, but we should also check our changes to index.js in the browser. Because you've added the addEventListener() function inside the addingEventListener() function, recall that you will need to call the outer function in index.js to execute addEventListener() and activate the event listener. Be sure to refresh the page to load the new code in index.js.

Resources




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap