Home >Web Front-end >JS Tutorial >jQuery uses each method and for statement to traverse an array example

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

高洛峰
高洛峰Original
2016-12-29 10:55:201726browse

The example in this article describes how jQuery uses the each method and for statement to traverse an array. Share it with everyone for your reference, the details are 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){
  //下面两种方式实现的效果是一样的
    //result=result.concat(array[i]+" ; ");
    result=result.concat(n+" ; ");
  });
  $("#eachFunc").html(result);
}
</script>
<title>无标题文档</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>

I hope this article will be helpful to everyone in jQuery programming.

For more jQuery examples of using each method and for statement to traverse an array, please pay attention to the PHP Chinese website!


Statement:
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