jquery-常用方法

关闭页面/刷新页面事件:

$(function () {
            $(window).bind('beforeunload', function () {
                return '您输入的内容尚未保存,确定离开此页面吗?';
            });
        });

获取屏幕分辨率:

alert('分辨率:'+screen.width+'/'+screen.height);

 获取时间

function getLocalTime() {
    var myDate = new Date(); //时间实例
    var H = myDate.getHours(); //获取小时
    var M = myDate.getMinutes(); //获取分钟
    var S = myDate.getSeconds(); //获取秒
    var MS = myDate.getMilliseconds(); //获取毫秒

    var milliSeconds = H * 3600 * 1000 + M * 60 * 1000 + S * 1000 + MS;
    return milliSeconds;
}

保留两位小数

alert(parseFloat(10.353).toFixed(2));

ajax格式

$.ajax({
    type: "post",
    url: "ssss",
    contentType: "application/x-www-form-urlencoded; charset=utf-8",//解决中文乱码
    data: { step: "6" },
    dataType: "text",
    success: function (msg) { alert(msg); },
    error: function (XMLHttpRequest, textStatus, errorThrown) { }
});

获取元素节点属性

var title=$li.attr("title");

获取字符在字符串中的位置

说明:返回 String 对象内第一次出现子字符串的字符起始位置,从0开始,没有则返回-1:

var num = str.indexOf("?");

截取字符串

var  str = str.substr(num + 1); 

页面加入收藏夹

function addfav() {

    var url = 'http://172.16.50.52/yxwWeb/pages/index.aspx'

    var title = "易行网";

    if (document.all) {

        window.external.AddFavorite(url, title);

    } else if (window.sidebar) {

        window.sidebar.addPanel(title, url, "");

    } else if (window.opera && window.print) {

        var mbm = document.createElement('a');

        mbm.setAttribute('rel''sidebar');

        mbm.setAttribute('href', url);

        mbm.setAttribute('title', title);

        //mbm.click();

    }

    else {

        alert("浏览器不支持直接加入收藏夹,请手动添加。");

    }

    window.location.reload();

}

添加删除元素

//复制tbody模快

    $("#companyAdd").click(function () {

        $(this).parent().parent().parent().clone(true).appendTo("#tableCompany");

    });

    //删除tbody模快

    $("#companydel").click(function () {

        //判断tbody个数

        var count = $("#tableCompany tbody").length;

        if (count > 3) {

            $(this).parent().parent().parent().remove();

        }

        else {

            alert("请最少添加一组");

        }

    });

判断浏览器

function browserJudge() {

    //IE

    if ($.browser.msie) {

        //如果是ie6

        if ($.browser.version == "6.0") {

            alert("请使用IE8以上的浏览器,或者切换到IE兼容模式");

            window.location = "../default.aspx";

        }

        // $(function () { alert($.browser.version); });

    }

    //谷歌

    else if ($.browser.safari) {

        // $(function () { alert("this is safari!"); });

    }

    //火狐

    else if ($.browser.mozilla) {

         // alert("暂不支持火狐");

        //  $(function () { alert("this is mozilla!"); });

    }

    //欧朋

    else if ($.browser.opera) {

        // $(function () { alert("this is opera"); });

    }

    else {

        // $(function () { alert("i don't konw!"); });

    }

}

使用jQuery操作Cookies

首先用引用jquery.jscookie.js

创建Cookie:

 $.cookie(cookie_name, $("#username").val(), { path: '/', expires: 10 });

//expires设置cookie的过期时间,path设置cookie保存路径,’/‘为根目录

 获取cookie

 alert($.cookie("cookie_name"));

删除Cookie 

 $.cookie("cookie_name"null); 

设置cookie一小时后过期

 var cookietime = new Date();

 cookietime.setTime(date.getTime() + (60 * 60 * 1000)); //coockie保存一小时 

 $.cookie("example""foo", { expires: cookietime }); 

Jquery操作Array数组

var btn = $("#btn");

            var items = $(".check");

            btn.click(function () {

                var arrays = new Array();

                $.each(items, function (i, item) {

                    if (item.checked) {

                        arrays.push(i);

                    }

                });

                alert(arrays.length);

            });

Jquery操作Array数组

table隔行变色

 $("#tb tr:even").css("background","#888"); 


is事件

说明:点击button显示/隐藏下面的div

代码:

 $("#btn").click(function () {

                var content = $(this).next();

                if (content.is(":visible")) {

                    content.hide();

                }

                else {

                    content.show();

                }

            });

toggle切换事件

代码:

 $(function () {

            $("#btn").toggle(function () { alert("不爱我"); }, function () { alert("爱我"); });

        });

hover悬停事件

说明:hover(entet,leaver)方法用于模拟光标悬停事件

代码:

$(function () {

            $("#btn").hover(function () { alert("不爱我"); }, function () { alert("爱我"); });

        });

操作样式

获取样式

  $("div").attr("class");

设置样式

 $("div").attr("class""highLight");

追加样式

 $("div").addClass("highLight");

删除全部样式

 $("div").removeClass();

删除指定样式

 $("div").removeClass("highLight");

重复切换样式

$("div").toggleClass("highLight");

判断元素是否含有样式

 $("div").hasClass("highLight");

 $("div").is(".highLight");

one一次性事件

 $("#btn").one("click",function () {

                alert(11);

            });

绑定事件/移除事件

           //绑定事件

            $("#btn").bind("click", myfun1 = function () {

                alert(11);

            });

            //移除事件

            //第一个参数是数据类型,第二个参数是将要删除的函数,如果没有参数则删除所有绑定的事件

            $("#btn1").click(function () { $("#btn").unbind(); });


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值