分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

js中用for循环事件绑定的小问题

发布时间:2023-09-06 01:43责任编辑:苏小强关键词:js

在js中,如果用for循环进行事件绑定,可能会遇到一点小问题,看下面第一个示例,无论点击哪个div,都会弹出3,即length。

因为这相当于事件绑定的同时,并没有把所对应的i进行一起绑定,i的值是最后一个值,即3。

示例1

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<meta name="viewport" content="width=device-width, initial-scale=1.0"> ???<meta http-equiv="X-UA-Compatible" content="ie=edge"> ???<title>Document</title> ???<style> ???????div { ???????????width: 100px; ???????????height: 100px; ???????????background: red; ???????????margin-bottom: 3px; ???????} ???</style> ???<script> ???????window.onload = function () { ???????????var add_the_handlers = function (nodes) { ???????????????var i; ???????????????for (i = 0; i < nodes.length; i++) { ???????????????????nodes[i].onclick = function (e) { ???????????????????????alert(i); ???????????????????}; ???????????????} ???????????}; ???????????var divs = document.getElementsByTagName(‘div‘); ???????????add_the_handlers(divs); ???????} ???</script> ???<body> ???????<div></div> ???????<div></div> ???????<div></div> ???</body></html>

再看下面两个例子,通过事件绑定的同时,通过函数调用而不是函数定义进行i与事件的绑定。

示例2

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<meta name="viewport" content="width=device-width, initial-scale=1.0"> ???<meta http-equiv="X-UA-Compatible" content="ie=edge"> ???<title>Document</title></head><style> ???div { ???????width: 100px; ???????height: 100px; ???????background: red; ???????margin-bottom: 3px; ???}</style><script> ???window.onload = function () { ???????var add_the_handlers = function (nodes) { ???????????var helper = function (index) { ???????????????return function (e) { ???????????????????alert(index); ???????????????}; ???????????}; ???????????var i; ???????????for (i = 0; i < nodes.length; i++) { ???????????????nodes[i].onclick = helper(i); ???????????} ???????}; ???????var divs = document.getElementsByTagName(‘div‘); ???????add_the_handlers(divs); ???}</script><body> ???<div></div> ???<div></div> ???<div></div></body></html>

示例3

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<meta name="viewport" content="width=device-width, initial-scale=1.0"> ???<meta http-equiv="X-UA-Compatible" content="ie=edge"> ???<title>Document</title> ???<style> ???????div { ???????????width: 100px; ???????????height: 100px; ???????????background: red; ???????????margin-bottom: 3px; ???????} ???</style> ???<script> ???????window.onload = function () { ???????????var add_the_handlers = function (nodes) { ???????????????var i; ???????????????for (i = 0; i < nodes.length; i++) { ???????????????????(function (i) { ???????????????????????nodes[i].onclick = function () { ???????????????????????????alert(i); ???????????????????????}; ???????????????????})(i); ???????????????} ???????????}; ???????????var divs = document.getElementsByTagName(‘div‘); ???????????add_the_handlers(divs); ???????} ???</script> ???<body> ???????<div></div> ???????<div></div> ???????<div></div> ???</body></html>

js中用for循环事件绑定的小问题

原文地址:https://www.cnblogs.com/greatfish/p/7757656.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved