首页 > 代码库 > Js 中常用方法

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 = http://www.mamicode.com/"" + url_ + "\"/>";                $("#tbUrlList").prepend("<tr><td >" + img + "</td><td><input type=‘text‘ readonly=‘readonly‘ value=http://www.mamicode.com/‘" + url_ + "‘/></td><td><input value=http://www.mamicode.com/‘复制地址‘ 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必须唯一,如重复,则覆盖。

Js 中常用方法