天天看点

js常用方法总结(以后遇到再进一步总结)

1、index()    返回指定元素相对于其他指定元素的 index 位置。

<h1>店铺评价</h1>
            <div class="star starcd">
                <div>相符程度</div>
                <span ></span>
                <span ></span>
                <span></span>
                <span ></span>
                <span ></span>
            </div>
       //打星评价
       $('.starcd span').click(function(){
            var index = $(this).index();
            $('.starcd span').each(function(i,e){
                if(i<index) {
                    $(e).addClass('active');
                }else {
                    $(e).removeClass('active');
                }
            })
        });      

2、each()  规定为每个匹配元素规定运行的函数。 提示:返回 false 可用于及早停止循环。

$(selector).each(function(index,element))        index - 选择器的 index 位置     element - 当前的元素(也可使用 "this" 选择器)