javascript边角知识

1.组织默认事件

阻止默认事件,h5默认的input type='date'在某些浏览器和android设备上没有效果,这时要调用h5+的时间选择器,但是要组织input默认的click事件,代码如下:

    //选择时间
    $("#end_time").on("click",function(event){
        event.preventDefault();
        plus.nativeUI.pickDate( function(e){
            var d = e.date;
//            console.log(d.Format('yyyy-MM-dd'));
            $("#end_time").val(d.Format('yyyy-MM-dd'));
        },function(e){
            console.log( "未选择日期:"+e.message );
        },{title:"请选择到期时间",minDate:new Date()});    
    });

2.判断input type='checkbox' 是否被选中,代码如下

    if (!$("#shopregister #checkaggree").is(":checked")) {

        alert("请同意注册协议");
        return false;
    }

3.获取多个checkbox中被选中的那个的值,代码如下

<input name='is_refund' id='refund_1' type='radio' value='1' />
<input name='is_refund' id='refund_0' checked='checked' type='radio' value='0' />
$("#shopregister input[name='is_refund']:checked").val();

4.设置checkbox选中

$("[name='checkbox']:even").attr("checked",'true'); //如果这货不管用请用prop

5.获取多张图片中title属性的值

    user.id_pic1 = $($("#shopregister .id_pic")[0]).attr("title");
    user.id_pic2 = $($("#shopregister .id_pic")[1]).attr("title");
    user.id_pic3 = $($("#shopregister .id_pic")[2]).attr("title");

 6.ajax提交过程中显示进度图片

    $.ajax({
        type: 'POST',
        url: configManager.RequstUrl + "/api/user/createstore",
        data: postdata,
        beforeSend:function(){ $("#waitingupload").removeClass("heisebghid").addClass("heisebg");}
    }).done(function (data) {
        $("#waitingupload").removeClass("heisebg").addClass("heisebghid");
        if ("success" == data.state) {
           //服务端成功
        }
        else {
            //服务端失败
        }
    }).fail(function () {
        //ajax请求失败
    });

 7.选择器first和first-child的区别

<table>
<tr><td>Row 1</td></tr>
<tr><td>Row 2</td></tr>
<tr><td>Row 3</td></tr>
</table>

<script>$("td:first-child或者first").css("color", "red");</script>

first:Row1为红色
first-child:三个Row都为红色
分别表示:第一个td 和 每个td的第一个元素

 

8.jquery判断元素是否隐藏

$("#givegoodsback").is(":hidden");可以,$("#givegoodsback").is(":visible");不行的

 

8.判断变量类型

     var   gettype=Object.prototype.toString

        gettype.call('aaaa')        输出      [object String]

        gettype.call(2222)         输出      [object Number]

        gettype.call(true)          输出      [object Boolean]

        gettype.call(undefined)  输出      [object Undefined]

        gettype.call(null)                  输出   [object Null]

         gettype.call({})                   输出   [object Object]

         gettype.call([])                    输出   [object Array]
         gettype.call(function(){})     输出   [object Function]

      看到这里,刚才的问题我们解决了。 

      其实js 里面还有好多类型判断      [object HTMLDivElement]     div 对象  ,    [object HTMLBodyElement]  body 对象    ,[object Document](IE)或者  [object HTMLDocument](firefox,google) ......各种dom节点的判断,这些东西在我们写插件的时候都会用到。

     可以封装的方法如下  :

var gettype=Object.prototype.toString
var utility={
    isObj:function(o){
       return    gettype.call(o)=="[object Object]";
    },

    isArray:function(o){
       return    gettype.call(o)=="[object Array]";
    },

    isNULL:function(o){
       return    gettype.call(o)=="[object Null]";
    },

    isDocument:function(){
        return    gettype.call(o)=="[object Document]"|| [object HTMLDocument];
    }
    ........
}

 

9.使用is判断是否为id=XXX的元素

if ( $(obj).parent().is('#fancybox-content') === true) {
    busy = false;
    return;
}

 

10.一次添加多个元素

        outer.append(
            content = $('<div id="fancybox-content"></div>'),
            close = $('<a id="fancybox-close"></a>'),
            title = $('<div id="fancybox-title"></div>'),

            nav_left = $('<a href="javascript:;" id="fancybox-left"><span class="fancy-ico" id="fancybox-left-ico"></span></a>'),
            nav_right = $('<a href="javascript:;" id="fancybox-right"><span class="fancy-ico" id="fancybox-right-ico"></span></a>')
        );

 11.添加cookie并设置失效时间

                            var expires = new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 60)); //60天
                            Ext.util.Cookies.set('autoLogin', autoLogin.getValue(), expires);
                            Ext.util.Cookies.set('loginUserId', userId, expires);
                            Ext.util.Cookies.set('loginPassword', password, expires);

 

posted @ 2015-07-07 11:03  nd  阅读(1021)  评论(0编辑  收藏  举报