jQuery uses the each method and for statement to traverse an array example

  • 2021-06-28 10:49:31
  • OfStack

This article provides an example of jQuery traversing an array using the each method and for statements.Share it for your reference, as follows:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery-1.6.2.js" type="text/javascript"></script>
<script type="text/javascript">
var array=["huangbiao","24","boy"];
var length = array.length;
function forFunc(){
  var result = "";
  for(var i = 0; i < length; i++){
    result=result.concat(array[i]+" ; ");
  }
  $("#forFunc").html(result);
  return true;
}
function eachFunc(){
  var result = "";
  $(array).each(function(i,n){
  // The effect of the following two ways is 1 Sample 
    //result=result.concat(array[i]+" ; ");
    result=result.concat(n+" ; ");
  });
  $("#eachFunc").html(result);
}
</script>
<title> Untitled Document </title>
</head>
<body>
  <button onclick="forFunc()">forFunc</button><br>
  <div id="forFunc"></div>
  <button onclick="eachFunc()">eachFunc</button><br>
  <div id="eachFunc"></div>
</body>
</html>

More readers interested in jQuery-related content can view this site's topics: jQuery Common Plug-ins and Usage Summary, Ajax Usage Summary in jquery, jQuery Table (table) Operation Skills Summary, jQuery Drag and Skills Summary, jQuery Extension Skills Summary, jQuery Common Classic Skills Summary, andjQuery Animation and Utility Summary and jquery Selector Usage Summary

I hope that the description in this paper will be helpful to everyone's jQuery program design.


Related articles: