js each函数 的使用

24 篇文章 0 订阅

使用场景1. $().each()

当存在多个相同的class元素,元素是不固定的,并且要给他们增加点击事件

$(".tip").each(function (index) {
                    $(this).on("click", function () {                   
                       //循环给每个元素增加背景颜色
                        $(".tip").each(function () {    
                            $(this).css("background-color", "transparent");
                        });
                        $(this).css("background-color", "#1E90FF");    
                        var lc_name = $(this).attr("name");
                        var lon = $(this).attr("lon");
                        var lat = $(this).attr("lat");
                        var azimuth = $(this).attr("azimuth");
                        var bandclass = $(this).attr("bandclass");
           locateSector(lon,lat,covercharacter,azimuth,bandclass);
                    });
                });

使用场景2. $.each

第一个参数为dom元素对象

$.each($('.country_info'),function(index,item){
                switch(index){
                    case 0:
                    /*根据选择的指标去展示对应的指标*/
                    var goalObj = $('#shwo_goal');
                    goalObj.text(data[0].paixuType);
                    $(item).text(data[0].SORT);
                    break;
                }
            });

第一个参数为data数组

$.each(data, function (index, item) {
                if (index == 0)
                return true;
                str += '<div class="province_china label fl province_flag" style="color:#fff;cursor: default;">\
                <div class="icon_location deleteAllowed'+ (index==1?' circle':(index==2?' circle':(index==3?' circle':''))) +'">\
                <img  alt="省图标" style="display: none;position: absolute;top: 0.625rem;" class="yellow_icon">\
                <span class="topthree'+ (index==1?' colorquan':(index==2?' colorquan':(index==3?' colorquan':''))) +'" style="position: absolute;top: -3px;left: 7px;" class="white_icon icon_color">' + index + '</span>\
                </div>\
                <ul style="width: 12.5rem;position: absolute;left: 4.6rem;" data-type="province" class="list_item" data-name="' + item.province + '">\
                <li>\
                <a href="javascript:void(0)" class="item_color"><span>'+ item.province +'</span><span class="fr">'+ item.SORT +'</span></a>\
                </li>\
                </ul>\
                </div>'
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值