Js 中常用方法

时间:2023-03-08 19:23:01

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

 function newGuid() {
var guid = "";
var n = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
for (var i = 1; i <= 8; i++) {
guid += n;
}
return guid;
}

二、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");
}
})
}
});

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

 //id : button控件id ,txt:要复制的内容
function copyToClipboard(id, txt) {
var clip = new ZeroClipboard.Client();
clip.setHandCursor(true);
clip.setText(txt);
clip.glue(id);
clip.addEventListener("complete", function () {
alert("复制成功!");
});
}

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

四、Iframe 刷新

document.location.reload();

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

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

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

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