javascript - 怎样用jQuery写这段代码
伊谢尔伦
伊谢尔伦 2017-04-11 11:09:20
[JavaScript讨论组]
window.onload=function(){
            var oUl=document.getElementById('oUl');
            var aLi=oUl.children;

            for(var i=0;i<aLi.length;i++){
                aLi[i].index=i;
                aLi[i].onmouseover=function(){
                    for(var i=0;i<aLi.length;i++){
                        if(this.index>=i){
                            aLi[i].className='ac';
                        }else{
                            aLi[i].className='';
                        }
                    }
                };
            }

        } 
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(9)
怪我咯
$("#oUL li").click(function(){
    $(this).attr("class", "ac")
        .siblings()
        .attr("class", "")
})
ringa_lee
var aLi = $('#oUl li')
aLi.on('mouseover', function(){
    aLi.each(function(index, value){
        $(this).index >= index? $(this).attr('class', 'ac'): $(this).attr('class', '');
    }) 
})
PHPz
$(function() {
    $('#oUl').on('mouseover', 'li', function(e) {
        var $t = $(this);
        
        $t.siblings().removeClass('ac');
        $t.addClass('ac').nextAll().addClass('ac');
    });
})
怪我咯

不知道你的需求,只是把你的代码翻译了一边,代码量貌似变多了,真是糟糕...

$(document).ready(function () {
  var oLi = $('#oUl li')
  oLi.each(function (index, item) {
    item.index = index
    item.onmouseover = function () {
      $(item).each(function (index1, item1) {
        if($(item).index >= index1){
          $(item1).addClass('ac')
        }else{
          $(item1).removeClass('ac')
        }
      })
    }
  })
})
ringa_lee
$(function(){
    var oULchild=$("#oUL *");
    
    oULchild
        .each(function(index){
            $(this)[0].index=index;
        })
        .on("mouseover",function(){
            oULchild.each(function(index){
                $(this)[0].index>=index?$(this).addClass('ac'): $(this).removeClass('ac');
            });
        })
    
})

不知道你要做什么,如果是给鼠标所在的子对象添加class,移除其他子对象的class那就是第一个答案那样:

$(function(){
    $("#oUL *").on("mouseover",function(){
        $(this).addClass("ac")
               .siblings()
               .removeClass("ac")
    })
})
PHPz
$('#oUl > li').on('mouseover',function(){
    $('#oUl > li:lt(' + ($(this).index() + 1) + ')').attr('class','ac');
    $(this).nextAll().attr('class','');
})
迷茫

一行解决:

$('#oUl > *').on('mouseover', function() {
    $(this).prevAll().attr('class', '').end().nextAll().addBack().attr('class', 'ac');
});
迷茫

不是最简洁的,但是能表达出你的意思

    $(function(){
        var aLi = $("#oUl li");
        aLi.each(function() {
            $(this).mouseover(function() {
                $(this).addClass('ac').prevAll().addClass('ac').end().nextAll().removeClass('ac');
            });
        });
    })
PHP中文网
$('#oUl > li').on('mouseover',function(){
   $(this).prevAll().attr('class', '').end().nextAll().attr('class', 'ac');
})
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号