自己项目一些用到的jq方法

1,这样可以给未来元素添加事件

$(document).on("click", ".title-r", function() {}

2,输入框输入事件,感觉比聚焦好用,限制字数,根据输入字数显示长度

$(".text-box").bind("input propertychange",function(){
        var put = $(this).next(".num").find('.put');
        var max = $(this).next(".num").find('.max');
        var val = $(this).val();
        var lgh = $(this).val().length;
        var maxlgh = parseInt($(max).text());
        if(lgh>maxlgh){
            $(this).val(val.substring(0,maxlgh));
            $(put).text(maxlgh);
        }else{
            $(put).text(lgh);
        }
    });

3,change事件,用在input:file这里了

$(".file-input").change(function(){
        var val = $(this).val();
        if(!val==""){
            $(this).next(".file-img").removeClass("hide").attr("src",val);
            $(this).next().next(".del-img").removeClass("hide");
        }
    });

4,滚动条事件,让一块dom随着滚动条事件触发定位效果

$(window).scroll(function() {
                if($(window).scrollTop() > 135) {
                    $('.new-r-phone').css({
                        "position": "fixed",
                        "margin": "-130px 0 0 0"
                    })
                }else{
                    $('.new-r-phone').css({
                        "position": "relative",
                        "margin": "0 0 0 0"
                    })
                }
            });

5,获得高度,设置高度

var height = $(".newtask-l").height();
$(".newtask-r").height(height);

6,引入公共部分,比如头部

$(document).ready(function() {
    $(".header").load("public-header.html");
});

7,遍历,我这是遍历的单选复选框

$.each($('input:checkbox,input:radio'), function() {
    if(this.checked) {
        $(this).next('b').css({
            background: "#5C6AC0",
            border: "none",
            width: "17px",
            height: "17px"
        })
    }
});

8,给select下拉加事件,用的change,给选项加上val值

$(".send-way").bind("change", function() {
    if($(this).val() == 1) {
        $(".news-center,.up-pic").addClass('hide')
    }
    if($(this).val() == 2) {
        $(".news-center").removeClass('hide')
        if($(".news-model").val() == 2) {
            $(".up-pic").removeClass('hide')
        }
    }
})

9,获得下标,或者通过下标选择之后进行操作

var index = $(this).parent().parent().index();
$('.pcd').eq(index).remove();

10,find和childeren的区别,find是找到子元素里面所有符合条件的,包括子元素里面的子元素,children是找到亲子元素,不会往子元素下面继续找

猜你喜欢

转载自www.cnblogs.com/zongyuan/p/8981639.html