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" 选择器)

3、prev() / next()  指定元素的上一个元素/指定元素的下一个元素

4、parent()/children()  指定元素的父元素/指定元素的子元素

5、siblings()  返回被选元素的所有同级元素、同级元素是共享相同父元素的元素

猜你喜欢

转载自www.cnblogs.com/zouhong/p/11871237.html