常用jQuery代码01

1.点击获得当前元素索引,实现切换相应的图片路径

$(".li").bind("click", function () {
            var _num = $(this).index();//或者 var _num = $(".li").index($(this));
            $(".pic_poster_img img").attr("src", "images/live/mews_pic" + (_num + 1) + ".jpg");
        });

 

 

 

2.窗口改变时调整元素宽度

$(function () {
     //页面加载执行宽度微调
     TextWidthResponsive();
    //窗口改变执行宽度微调
      $(window).resize(function () {
          TextWidthResponsive();
     });
});
function TextWidthResponsive() {
    var _width = $(window).width() - 1050;
    $(".focus_title_box").width(_width);
}



3.手机号码验证

var _phone = $(".phone").val();
            _name = $.trim(_name);
            _phone = $.trim(_phone);

            if (_name == "") {

                alert("请输入姓名!");
                return;
            }
            

            if (_phone == "") {
                alert("请输入联系方式!");
                return;
            }

            var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$//;                  

            if (!(reg.test(_phone))) {
                alert('对不起,您输入的手机号码错误。');
                return;
            } 

4.对移动端动态填充的元素绑定点击事件

 //选择答案
            $("body").undelegate("touchstart").delegate(".option", "touchstart", function (e) {
                $(".option").removeClass("hover");
                $(this).addClass("hover");

            });

  

posted @ 2015-04-16 19:53  若强  Views(187)  Comments(0Edit  收藏  举报