jquery each index

获取index

Java代码  

  1. <div class="side-menu">  
  2.     <h3 class="item">管理中心</h3>  
  3.     <div class="menus">  
  4.         <p class="title title-close"><span class="grep">订单中心</span><i class="radius"></i></p>  
  5.         <ul style="display:none;">  
  6.             <li><a href="###">我的订单</a></li>  
  7.             <li><a href="###">我的关注</a></li>  
  8.             <li><a href="###">浏览记录</a></li>  
  9.             <li><a href="###">为我推荐</a></li>  
  10.         </ul>  
  11.         <p class="title title-close"><span class="grep">客户服务</span><i class="radius"></i></p>  
  12.         <ul style="display:none;">  
  13.             <li><a href="###">返修/退货</a></li>  
  14.             <li><a href="###">取消订单记录</a></li>  
  15.             <li><a href="###">我的投诉</a></li>  
  16.         </ul>  
  17.         <p class="title"><span class="grep">账户中心</span><i class="radius"></i></p>  
  18.         <ul>  
  19.             <li><a href="###" class="cur">账户信息</a></li>  
  20.             <li><a href="###">账户安全</a></li>  
  21.             <li><a href="###">账户余额</a></li>  
  22.             <li><a href="###">消费记录</a></li>  
  23.             <li><a href="###">我的积分</a></li>  
  24.         </ul>  
  25.     </div>  
  26. </div>  
  27. <script type="text/javascript">  
  28.     $(".menus > p").click(function(){  
  29.         var index = $('p').index($(this));  
  30.         $(this).siblings('p').removeClass("title").addClass("title title-close");  
  31.         $(this).parent().find('ul').eq(index).siblings('ul').hide();  
  32.         $(this).removeClass("title title-close").addClass("title");  
  33.         $(this).parent().find('ul').eq(index).show();  
  34.     });  
  35. </script>  

 

JQ的each方法callback回调函数拥有两个默认(可省略)参数:第一个为对象的属性  数组的索引 , 第二个为对应属性的值  索引的内容

当激活的html样式(高亮)与其他html样式差别太大时,用div来实现(big small)

 

Java代码  

  1. <ul id="hottop5" class="middle2">  
  2.     <li class="middle1">  
  3.         <div style="display: none;" class="small">  
  4.             <p class="miaoshu"><strong>1/</strong>  
  5.                 <a href="http://www.test.com/-4000.html">  
  6.                     游戏</a>  
  7.             </p>  
  8.         </div>  
  9.         <div class="big" style="display: block;">  
  10.             <div class="no1_a">  
  11.                 <p class="no1 alignleft">1<span>/</span></p>  
  12.   
  13.                 <p class="no1_img">  
  14.                     <a href="http://www.test.com/-4000.html">  
  15.                         <img src="http://media.test.com/catalog/product/cache/3/small_image/55x55/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/small_image.jpg">  
  16.                     </a>  
  17.                 </p>  
  18.             </div>  
  19.             <div class="no1_w clear">  
  20.                 <a href="http://www.test.com/-4000.html">  
  21.                     游戏  
  22.                 </a></div>  
  23.         </div>  
  24.     </li>  
  25.   
  26.     <li class="middle1">  
  27.         <div class="small" style="display: block;">  
  28.             <p class="miaoshu"><strong>2/</strong>  
  29.                 <a href="http://www.test.com/catalog/product/view/id/4001/">  
  30.                     颜色尺码</a>  
  31.             </p>  
  32.         </div>  
  33.         <div style="display: none;" class="big">  
  34.             <div class="no1_a">  
  35.                 <p class="no1 alignleft">2<span>/</span></p>  
  36.   
  37.                 <p class="no1_img">  
  38.                     <a href="http://www.test.com/catalog/product/view/id/4001/">  
  39.                         <img src="http://media.test.com/catalog/product/cache/3/small_image/55x55/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/small_image.jpg">  
  40.                     </a>  
  41.                 </p>  
  42.             </div>  
  43.             <div class="no1_w clear">  
  44.                 <a href="http://www.test.com/catalog/product/view/id/4001/">  
  45.                     颜色尺码  
  46.                 </a></div>  
  47.         </div>  
  48.     </li>  
  49.   
  50.     <li class="middle1">  
  51.         <div class="small" style="display: block;">  
  52.             <p class="miaoshu"><strong>3/</strong>  
  53.                 <a href="http://www.test.com/-3999.html">  
  54.                     电银票优惠券</a>  
  55.             </p>  
  56.   
  57.             <p class="dian">100.00 兑兑点</p>  
  58.         </div>  
  59.   
  60.         <div style="display: none;" class="big">  
  61.             <div class="no1_a">  
  62.                 <p class="no1 alignleft">3<span>/</span></p>  
  63.   
  64.                 <p class="no1_img">  
  65.                     <a href="http://www.test.com/-3999.html">  
  66.                         <img src="http://media.test.com/catalog/product/cache/3/small_image/55x55/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/small_image.jpg">  
  67.                     </a>  
  68.                 </p>  
  69.             </div>  
  70.             <div class="no1_w clear">  
  71.                 <a href="http://www.test.com/-3999.html">  
  72.                     电银票优惠券  
  73.                 </a></div>  
  74.         </div>  
  75.     </li>  
  76. </ul>  
  77. <script type="text/javascript">  
  78.     $("#hottop5 > li").each(function (i) {  
  79.         $(this).mouseover(function () {   
  80.             $(this).siblings().find(".small").show();// 其他显示小图  
  81.             $(this).find(".small").hide();  
  82.             $(this).siblings().find(".big").hide();  
  83.             $(this).find(".big").show(); //显示当前大图  
  84.         })  
  85.     })  
  86. </script>   

你上面的例子, 属于数组(对象数组)形式,如果加参数,第一个参数就是数组的索引值(0,1,2,....),第二个参数为索引下的对象(DOM)。

Java代码  

  1. var arr1 = [ "one", "two", "three", "four", "five" ];  
  2. $.each(arr1, function(){  
  3.     alert(this);  
  4. });  
  5. 输出:one   two  three  four   five  
  6. var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]  
  7. $.each(arr2, function(i, item){  
  8.     alert(item[0]);  
  9. });  
  10. 输出:1   4   7  
  11. var obj = { one:1, two:2, three:3, four:4, five:5 };  
  12. $.each(obj, function(key, val) {  
  13.     alert(obj[key]);        
  14. });  
  15. 输出:1   2  3  4  5  
时间: 2024-08-10 05:27:50

jquery each index的相关文章

JQuery使用index方法获取Jquery对象数组下标的方法

  本文实例讲述了JQuery使用index方法获取Jquery对象数组下标的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

jQuery中index()方法用法实例_jquery

本文实例讲述了jQuery中index()方法用法.分享给大家供大家参考.具体分析如下: 此方法可以搜索匹配元素,并返回元素的索引值. 索引值是从0开始的. 语法结构一: 当此方法没有参数的时候,返回值是指定元素在其同辈元素集合中的索引位置. 复制代码 代码如下: $(selector).index() 实例代码: 实例一: 复制代码 代码如下: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8&quo

jQuery中index()的用法分析_jquery

本文实例讲解了jQuery中index()的用法.分享给大家供大家参考之用.具体方法如下: 现在有这样一个问题:如果列表里面有N多的列表项目,我想知道我点击了哪一个应该怎么取呢? 对此,jQuery里面提供了一个index()方法 : index(subject) 该方法搜索与参数表示的对象匹配的元素,并返回相应元素的索引值. 如果找到了匹配的元素,从0开始返回:如果没有找到匹配的元素,返回-1. 但是 API里面提供的例子貌似不对,示例如下: <ul> <li><a hre

jquery通过index获取select下的option

问题描述 如图jquery这样通过index索引获取option时有时可以有时又不行怎么回事? 解决方案 解决方案二:同一个地方有时可以有时不可以吗?如果不是,那不可以的地方肯定写错了嘛解决方案三:你自己写写看看啊就导个jquery的包就行了我不知道是不是jquery包的版本的问题解决方案四:如果没有index>=3的,那就取不到解决方案五:这种低级错误我不会犯得是不是jquery包的问题看网上也没说这个的

jquery的index方法实现tab效果_jquery

左侧为选项卡,右侧为详细内容. 原理: 点击左侧的列表项,根据所选列表项在列表的[索引n]显示第n个内容. (首先需要将列表和内容一次性加载到页面,内容只显示第一个,这样切换起来更友好.) 如何获取选择列表项在列表中的索引: jquery里有一个方法是index([subject]) $("#ul li").index($("#selected")); 意思是 首先设定列表范围(#ul li列表),然后返回$("#selected")元素在列表中

jquery获取元素索引值index()方法

 这篇文章主要介绍了jquery获取元素索引值index()方法,需要的朋友可以参考下 jquery获取元素索引值index()方法:   jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数.    如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的位置.  如果参数是一组DOM元素或者jQuery对象,那么返回值就是传递的元素相对于原先集合的位置.  如果参数是一个选择器,那么返回值就是原先元素相对

JQuery index()方法使用代码_jquery

学生科的网站首页有19个Repeater控件.6个div块的tabs切换. 做tabs切换总不能一个个去写方式吧:(代码如下....) 复制代码 代码如下: $(function() { $("#tabs a:eq(0),#tabs2 a:eq(0),#dbt_l_t_tabs a:eq(0),#dbt_r_t_tabs a:eq(0)").addClass("m_on"); $("#tabs a:gt(0),#tabs2 a:gt(0),#dbt_l_t

jQuery实现获取元素索引值index的方法_jquery

本文实例讲述了jQuery实现获取元素索引值index的方法.分享给大家供大家参考,具体如下: <!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">

jquery获取元素索引值index()示例_jquery

jquery获取元素索引值index()方法: jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的位置. 如果参数是一组DOM元素或者jQuery对象,那么返回值就是传递的元素相对于原先集合的位置. 如果参数是一个选择器,那么返回值就是原先元素相对于选择器匹配元素中的位置.如果找不到匹配的元素,则返回-1. 复制代码 代码如下: <ul> <