My JavaScript study notes

21. How to Date objects to be 5 days after the date?
Var = new new myDate a Date ()
myDate. setDate(MyDate.getDate () + 5)
Note: If you increase the number of days will change the month or year, it will automatically accomplish this target date the converter

22. how to use the sort () method to sort the array from the value?
<HTML>
<body>
<Script type = "text / JavaScript">
function sortNumber (A, B)
{
return A - B
}

var ARR = new new the Array (. 6)
ARR [0] = "10"
ARR [. 1] = ". 5"
ARR [2] = "40"
ARR [. 3] = "25"
ARR [. 4] = "1000"
ARR [. 5] = ". 1"
document.write (ARR + "<br />")
document.write ( arr.sort (sortNumber) )
</ Script>
</ body>
</ html>
results are as follows:
10,5,40,25,1000,1
1,5,10,25,40,1000

If the arr.sort (sortNumber) sortNumber removed, the result is:
10,5,40,25,1000,1
1,10,1000,25,40,5
because sort () method to sort the array only literally.

23.js dynamically generate control

Example HTML code:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
        function add()
        {
            var target=document.getElementById("span1");
            var txt =document.getElementById("txt1");
            var nCount=parseInt(txt.value);
            nCount=nCount+1;
            txt.value=nCount;
            target.innerHTML=target.innerHTML+"<select id='select1'><option value='1'>1</option></select><br /><br />";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" value="添加" onclick="add()" /><input type="text" id="txt1" value="0" /><br />
        <span id="span1"></span>
    </div>
    </form>
</body>
</html>


24.JS achieved in C # Trim () function function

function ltrim(str)
{
 var position;
 position=str.length
 for (i=0;i<str.length;i++)
 {
  if (str.charAt(i)!=" ")
     {position=i;break;}
 }
 return (str.substring(position,str.length))
}
function rtrim(str)
{
 var position 
 position=str.length
 for (i=str.length-1;i>=0;i--)
     {if (str.charAt(i)!=" ") {position=i;break;}}
 return(str.substring(0,position+1))
}
//去除左右边空格
function trim(str)
{return ltrim(rtrim(str))}

 

Reproduced in: https: //www.cnblogs.com/guoxiaowen/archive/2008/02/27/1083296.html

Guess you like

Origin blog.csdn.net/weixin_34082789/article/details/93329977