Home > Web Front-end > JS Tutorial > body text

Problems related to the process of binding events to elements in batches in JS are solved using closures_jquery

WBOY
Release: 2016-05-16 17:36:58
Original
883 people have browsed it

In JS, when you write a for loop, the internal loop variable I will actually be saved in the function or class it runs, so you will find that when you batch-bind events to elements, i = the last one appears. The value of the loop variable is very confusing. There are two solutions.
The idea is: save this loop variable, do not let its scope be in the entire function, but within the loop body

1. Use closures (if you don’t understand closures, please open Baidu: www.baidu.com)
2. Use the with keyword, the with syntax is with(obj) { //so that you can directly use it here Access the properties of obj without adding obj.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template