Very like to write some small method or pseudo-code model to save up, you can directly take it, although still a rookie, but Grandpa is from the grandson came, the following stickers on the use of a few small examples this week
# #1. When you render elements dynamically, you need a logical judgment
When a DOM element is rendered in a loop, a logical operation is required in the loop traversal, if the template is not used:
var addlist = function () {
var lists= [],list= "";
$.each (Data,function (I,item) {
if (condition 1) {
list+= "<li>" +$ (item). val () + "</li>";
}
if (condition 2) {
list+= "<li>" +$ (item). val () + "</li>";
}
Lists.push (list);
});
$ ("#id"). HTML (Lists.join (""));
}
This is when looking at a colleague code, found her using push and join method, render elements, think not much is obviously even in the loop of the elements being rendered, it is convenient to carry out the logical operation, with list+= even more rendering elements, also makes the code very regular;
# #2. Create another JSON object from one of the JSON's corresponding properties
var createjsonobj=function (data) {
var jsonobj = [];
$.each (Data,function (I,item) {
var innerobj = {
Key: "",
Value: ""
};
Innerobj.key = item.id;
Innerobj.value = Item.name;
Jsonobj.push (Innerobj);
});
return jsonobj;
}
The above is to create a [{"Key": "XXX", "value": "XXX"},{"key": "XXX", "value": "XXX"}] JSON object of a simple small example, you can try to consider a better package, the key value of the incoming new object to implement the customization.
# #3. JS get some small operations for the date
There are also many examples of this online, this week mainly encountered the following two operations
var jsdate = {
Get current 2014-06-12 16:55 format date
Gettime:function (date) {
var year = "", month= "", day= "", hour= "", minute= "", Time= "", oldday= "";
if (date) {
Oldday=new Date (Date.gettime () -3600*1000*24*7);
Year= oldday.getfullyear ();
Month= oldday.getmonth () +1;
Day = Oldday.getdate ();
hour = Oldday.gethours ();
minute = Oldday.getminutes ();
}else{
var now= new Date ();
Year = Now.getfullyear ();
month = Now.getmonth () +1;
Day = Now.getdate ();
hour = Now.gethours ();
minute = Now.getminutes ();
}
if (month<10) {
Month= ' 0 ' +month;
}
if (day<10) {
day= ' 0 ' +day;
}
if (hour<10) {
hour = ' 0 ' + hour;
}
if (minute<10) {
Minute= ' 0 ' +minute
}
Time = (year+ "-" +month+ "-" +day+ "" +hour+ ":" +minute ");
return time;
},
Calculate the difference n days date
Subtime:function (date,n) {
Date = new Date (date)
Date = Date.valueof ()
Date = Date-n * 24 * 60 * 60 * 1000
Date = new Date (date)
Date = Jsdate.gettime (date);
return date;
}
}
The above example is to clarify some JS time of the 0 problem, and take the time to get a demo
# #4. When logic is judged many times, the code is structured in the following way
var bo1 = False,bo2 = False,bo3=false;
if (condition 1) {
Bo1 = false;
Logical Operation 1
}
if (condition 2) {
Bo2 = false;
Logical Operation 2
}
if (condition 3) {
Bo3 = false;
Logical Operation 3
}
if (Bo1&&bo2) {
Logical Operation 4
}
...
This may seem more annoying, but may be a personal habit, preferring this
This post was issued in a personal note in June, and there are many improvements that have not yet been collated ...
A way to save money.