前端杂记

来源:互联网 发布:警惕网络诈骗ppt 编辑:程序博客网 时间:2024/06/07 06:55

1.jQuery 取选中的radio的值方法

var val=$('input:radio[name="sex"]:checked').val();

附三种方法都可以:

$('input:radio:checked').val();
$("input[type='radio']:checked").val();

$("input[name='rd']:checked").val();

2.处理jQuery append加入的元素 绑定事件无效的方法

解决方法:用on函数绑定事件

格式: $("outerSelector").on('eventType','selector',function(){})

例如:$(document).on('click',"[name='submitbutton']",function(){....});


3.手机屏幕滑动事件

$(window).scroll(function(){
        if(($(window).scrollTop() + $(window).height() > $(document).height()-1)){
            console.log('你要执行的ajax');
        }
    });

4.遍历数组

4.1 forEach()

var arr = [1,2,3,4];
arr.forEach(function(value,index,array){
    array[index] == value;
    sum+=value;  
    });
4.2 $.each()
$.each([],function(index,value,array){
   //code something
})
  4.2 $().each()

$('div').each(function(index,element){

//code something

        })

原创粉丝点击