jQuery之each方法

简介:

代码:

复制代码
<script>
    $(document).ready(function(){
        //检测是否有lesson_id传入
        var lesson_id = "<!--{$lessonId}-->";
        if(lesson_id>0){
            $('.cursor').each(function(i){
                if($(this).attr('val') == lesson_id){
                    $(this).removeClass('cj_li').addClass('fw');//没有的话就不移除,有的话就不重复添加,多次点击依旧如此
                    $(this).siblings('.cursor').removeClass('fw').addClass('cj_li');
                }
            });
        }
        
        //切换查看
        $(".cursor").click(function(){
            $(this).removeClass('cj_li').addClass('fw');//没有的话就不移除,有的话就不重复添加,多次点击依旧如此
            $(this).siblings('.cursor').removeClass('fw').addClass('cj_li');
            //获取type
            var type = $("input[name=type]").val();
            //获取sequence
            var sequence = $("input[name=sequence]").val();
            //获取sequence_name
            var sequence_name = $("#seqName").text();
            //获取lesson_id值
            var lesson_id = $(this).attr('val');
            if("undefined"==typeof(lesson_id)){
                //获取全科排名
                if(type!=null&&sequence!=null&&sequence_name!=null){
                    location.href=("/exams/teacher/showclassstat/sequence/"+sequence+"/type/"+type+"/sequence_name/"+sequence_name);
                }
            }else{
                //获取单科排名
                if(type!=null&&sequence!=null&&sequence_name!=null&&lesson_id!=null){
                    location.href=("/exams/teacher/showclassstat/sequence/"+sequence+"/type/"+type+"/sequence_name/"+sequence_name+"/lesson_id/"+lesson_id);
                }
            }
            
            
            
        });
    });
</script>
复制代码

解析:

1 $('.cursor').each(function(i){
2                 if($(this).attr('val') == lesson_id){
3                     $(this).removeClass('cj_li').addClass('fw');//没有的话就不移除,有的话就不重复添加,多次点击依旧如此
4                     $(this).siblings('.cursor').removeClass('fw').addClass('cj_li');
5                 }
6             });

这里用到了each方法

对类名为cursor的元素集进行一一处理,

$(this)就代表这次循环的当前对象,

如果其属性的值与lesson_id相等,就进行样式处理。

正如注释所言,removeClass,如果没有可移除属性,就不移除。有点话,不会多次添加。



本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/archive/2013/03/12/2955811.html如需转载请自行联系原作者

相关文章
|
3月前
|
JavaScript
jQuery追加节点方法 和height方法与width方法
jQuery追加节点方法 和height方法与width方法
|
3月前
|
JavaScript 前端开发
调用jQuery的animate()方法无法移动的问题
调用jQuery的animate()方法无法移动的问题
|
3月前
|
JavaScript 前端开发 UED
jQuery 自动刷新页面但不闪烁的实现方法
jQuery 自动刷新页面但不闪烁的实现方法
|
4月前
|
JavaScript 前端开发
jQuery学习(十二)—jQuery中对象的查找方法总结
jQuery学习(十二)—jQuery中对象的查找方法总结
|
4月前
|
JavaScript
jQuery学习(九)—常用的包裹方法
jQuery学习(九)—常用的包裹方法
|
4月前
|
JavaScript
jQuery学习(七)— append方法与appendTo方法
jQuery学习(七)— append方法与appendTo方法
|
4月前
|
JavaScript
jQuery学习(八)—before方法、after方法、insertBefore方法、insertAfter方法
jQuery学习(八)—before方法、after方法、insertBefore方法、insertAfter方法
|
3月前
|
JavaScript 前端开发
jQuery特效函数-第7次课-show、hide等方法有动画效果的显示和隐藏一个元素-附案例-任务
jQuery特效函数-第7次课-show、hide等方法有动画效果的显示和隐藏一个元素-附案例-任务
19 0
|
3月前
|
JavaScript 前端开发
原生js与jQuery显示隐藏div的几种方法
原生js与jQuery显示隐藏div的几种方法
36 0
|
3月前
|
JavaScript 前端开发 CDN
jQuery文件下载方法及引入HTML语法
去jQuery网站下载文件包,点击主页的“Download”之后,进入下载页面,可以选择production版本的进行下载,但是点击进去之后,浏览器并不会直接下载相关的文件,而是跳转到一个“密密麻麻”都是jQuery代码的页面,仔细查看浏览器地址栏中的url便可知,该页面其实就是jQuery的min版的文件,可以使用下面这种方法,下载jQuery文件
64 5