How to Access Variables from jQuery's .each() Function
You can declare a variable outside the 'each' function and assign values to it inside the function. Here's an example:
var total = 0; // declare variable outside of each function
$('ul li').each(function() {
var value = parseInt($(this).text()); // get value of each li element
total += value; // add value to total
});
console.log(total); // use total variable outside of each function
In this example, we declared a variable 'total' outside the 'each' function. Inside the function, we retrieved the value of each 'li' element and added it to 'total'. Finally, we logged the 'total' variable outside the 'each' function.
原文地址: https://www.cveoy.top/t/topic/osLY 著作权归作者所有。请勿转载和采集!