Js 中常用方法

一、获取唯一值(2014-12-23)

1 function newGuid() {
2     var guid = "";
3     var n = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
4     for (var i = 1; i <= 8; i++) {
5         guid += n;
6     }
7     return guid;
8 }
View Code

二、ajaxfileupload.js(下载js插件)使用

$("#bttUp").click(function () {
    var value_temp = $("#fileUp").val();
    if (value_temp != "" && value_temp != null && value_temp != undefined && value_temp.length > 0) {
        var id = newGuid();
        var url_ = "";
        $.ajaxFileUpload({
            url: "../../Handler/ManagerHandler.ashx",
            type: "POST",
            fileElementId: "fileUp",
            dataType: "text/plain",
            data: { oprate: "upimg", type: $("#ddlImgType option:selected").val() },
            beforeSend: function () {
                alert("beforeSend");
            },
            complete: function () {
                copyToClipboard(id, url_);
            },
            success: function (data, status) {
                url_ = $(data).text();
                var img = "<img src = "" + url_ + ""/>";
                $("#tbUrlList").prepend("<tr><td >" + img + "</td><td><input type='text' readonly='readonly' value='" + url_ + "'/></td><td><input value='复制地址' type='button' id='" + id + "' class='copyurl' name='" + url_ + "'/></td></tr>");
            },
            error: function (data, status, e) {
                alert("error");
            }
        })
    }
});
View Code

三、ZeroClipboard.js(flash下载粘贴)使用

 1 //id : button控件id ,txt:要复制的内容
 2 function copyToClipboard(id, txt) {
 3     var clip = new ZeroClipboard.Client();
 4     clip.setHandCursor(true);
 5     clip.setText(txt);
 6     clip.glue(id);
 7     clip.addEventListener("complete", function () {
 8         alert("复制成功!");
 9     });
10 }
View Code

注意:传入的id必须唯一,如重复,则覆盖。

四、Iframe 刷新

document.location.reload();

 五、js 字符串转换成时间格式

 1 var tt = Date.parse('2015-11-11 11:11:11'.replace(/-/g, "/"));
 2 var tt  new Date(tt).Format("yyyy-MM-dd");
 3 
 4 // 对Date的扩展,将 Date 转化为指定格式的String   
 5 // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,   
 6 // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
 7 // 例子:   
 8 // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
 9 // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
10 Date.prototype.Format = function (fmt) { //author: meizz   
11     var o = {
12         "M+": this.getMonth() + 1,                 //月份   
13         "d+": this.getDate(),                    //
14         "h+": this.getHours(),                   //小时   
15         "m+": this.getMinutes(),                 //
16         "s+": this.getSeconds(),                 //
17         "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
18         "S": this.getMilliseconds()             //毫秒   
19     };
20     if (/(y+)/.test(fmt))
21         fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
22     for (var k in o)
23         if (new RegExp("(" + k + ")").test(fmt))
24             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
25     return fmt;
26 }
View Code

六、给img元素src属性赋值,IE下图片不显示:

注意代码块内不能有Console.log(); //不要问我为什么,各种实验,删了就行,加了就不显示。

原文地址:https://www.cnblogs.com/kongxp/p/4180727.html