原生JS
post方式进行页面跳转时间格式化扩展 json
获取json串的key数组使用方括号取值和使用点号取值的区别 jquery
闭包对象申明方式序列化form为json对象使用ajax的方式进行文件上传为JS动态生成的元素添加事件 小程序JS封装
wxrequest函数添加附加参数
原生JS
post方式进行页面跳转
Post:
function (URL, PARAMTERS) {
var temp_form = document.createElement(
"form");
temp_form.action = URL;
temp_form.target =
"_self";
temp_form.method =
"post";
temp_form.style.display =
"none";
for (
var item
in PARAMTERS) {
var opt = document.createElement(
"textarea");
opt.name = item;
opt.value = PARAMTERS[item];
temp_form.appendChild(opt);
}
document.body.appendChild(temp_form);
temp_form.submit();
}
时间格式化扩展
Date.prototype.format =
function(format){
var o = {
"M+" :
this.getMonth()+
1,
"d+" :
this.getDate(),
"h+" :
this.getHours(),
"m+" :
this.getMinutes(),
"s+" :
this.getSeconds(),
"q+" :
Math.floor((
this.getMonth()+
3)/
3),
"S" :
this.getMilliseconds()
}
if(
/(y+)/.test(format)) {
format = format.replace(
RegExp.$
1, (
this.getFullYear()+
"").substr(
4 -
RegExp.$
1.length));
}
for(
var k
in o) {
if(
new RegExp(
"("+ k +
")").test(format)) {
format = format.replace(
RegExp.$
1,
RegExp.$
1.length==
1 ? o[k] : (
"00"+ o[k]).substr((
""+ o[k]).length));
}
}
return format;
}
json
获取json串的key数组
var jsonStr = {houseId:
"12", valuationMethod:
"25", negotiatedPrice:
"152", valuationUnitPrice:
"12", discount:
"1"};
var keys = [];
for(
var jkey
in jsonStr){
keys.push(jkey);
}
使用方括号取值和使用点号取值的区别
从取值模型上来看,‘[]’取值方式是以数组模型进行取值,而‘.’取值则是以对象模型进行取值。在js中有一个特点:**关联数组就是对象,对象就是关联数组**。这也导致了在大多数情况下这两种取值方式没有什么区别,也就是说在大多数情况下两种取值方式都可以相互的替用。
然而在某些情况下还是不可替换的,使用对象取值模型也就意味着你已经知道了该json中所有的key值,然后可以在'.'号后面直接写出需要获取的值的key,这种情况下使用数组取值模型也是可以实现,只需要在[]中书写key值即可;在不明确json对象中key值时,或者需要动态的根据key(该值也不明确)值获取对应值的时候,使用对象模型取值就收到限制了,因为对象模型不允许将key设置为变量,所以这个时候只能使用数组模型获取,因为数组模型取值的索引值可以为变量,让使用者动态的输入。可以从下面状态机的代码中更清楚的认识到这个问题。
var status = {
1:
'激活',
2:
'锁定',
3:
'状态3',
4:
'状态4',
5:
'状态5'};
function getStatus(key){
if(key
in status){
return status[key];
}
else{
return undefined;
}
}
function getStatus(key){
if(key
in status){
}
else {
return undefined;
}
}
jquery
闭包对象申明方式
$(
function(w, $){
var myObject = {
variable1:
'',
method1:
function(){
}
};
w.myObject = myObject;
}(window, $));
serializeToJson:
function (formId, valExcepts) {
var vars = $(
"#" + formId).serializeArray();
var rtnJson = {};
if (valExcepts ==
undefined) {
valExcepts = [];
}
if(vars.length ==
0){
rtnJson =
undefined;
}
for (
var i =
0; i < vars.length; i++) {
var key = vars[i].name;
var value = vars[i].value;
if (!valExcepts.includes(key) && ( value ==
undefined || value ==
'' )) {
a.alertMessage($(
"[name=" + key +
"]").parent().find(
"p").html() +
"不能为空");
rtnJson =
undefined;
break;
}
else {
if (rtnJson[key] !==
undefined) {
if (!rtnJson[key].push) {
rtnJson [key] = [key];
}
rtnJson[key].push(value ||
'');
}
else {
rtnJson[key] = value ||
'';
}
}
}
return rtnJson;
}
使用ajax的方式进行文件上传
该方法上传文件需要使用到jquery.form.js插件
uploadFile:
function(formId){
$(
"#"+formId).ajaxSubmit({
type:
"post",
url:
"/sales/sales/uploadAttach",
success:
function (data) {
if (data) {
salesUtil.loadAttachs();
}
else {
a.alertMessage(
"系统错误,请联系管理人员!");
}
}
});
}
为JS动态生成的元素添加事件
要为JS动态生成(append, innerHTML and so on …)的元素添加事件有两种方式,一种事在代码生成后为其单独绑定事件,及在append之类的方法之后使用$(selector).click(func)之类的方法;还有一种就是我要记录的$(document.body).on(type, selector, func)来预先绑定事件,这种绑定方式是监听的当前网页所有元素,而第一种是绑定事件时刻的页面的指定元素。
var insertElementHtml =
'<a href="#" class="testElement"></a>';
$(document.body).append(insertElementHtml);
$(
'.testElement').unbind(
'click');
$(
'.testElement').click(
function(){alert(
"我就是一个测试按钮")});
$(document.body).on(
'click',
'.testElement',
function(){alert(
"我就是一个测试按钮"));
var insertElementHtml =
'<a href="#" class="testElement"></a>';
$(document.body).append(insertElementHtml);
小程序JS封装
wx.request函数添加附加参数
当前实现方法是在app.js中心书写一个request函数,参数与wx.request函数一致。在新的request函数中写入附加参数:
request:
function (parms) {
let data = {}
if (parms[
'header']) {
data = parms[
'header']
}
if(
this.globalData.buildingId){
data[
'BuildingId'] =
this.globalData.buildingId
}
else{
throw new Error(
"app.globalData.buildingId is not defined")
}
if (parms[
'auth']) {
if(!
this.globalData.userInfo){
this.getUserInfo(
function(data){
data[
'UserInfo'] = data
})
}
else{
data[
'UserInfo'] =
this.globalData.userInfo
}
}
parms[
'header'] = data
wx.request(parms)
}
转载请注明原文地址: https://ju.6miu.com/read-679572.html