javascript

一.引入javascript:

  1, 行内:  <button onclick = alert("点我了")></button>    #  行内写脚本, 声明动作, 执行方法

  2, 内接式: <script  type="text/javascript">

       var a = 2;

       console.log(s);       

         </script>

  3, 外接式:  <script   src= "位置"></script>

二 .定义变量 : var  name = value

  类型: string.  number  boolean  array null undefined

  tip: NaN :不是一个数

    parseInt  : 取整

    isNaN : 判断是否不是数字

    typeof : 判断变量的类型

    ++自加   a++   ++a  都能用

    = 赋值

    == 判断值是不是一样, 与类型无关

    === 判断类型和值是否一样

 

三  : 字符串拼接:

       1 +  "1"  >>> "11"

  1 + "" >>> "1"

  var name "背景"     我有"+ name+"   >>>> 我有背景

  ~我有${name} ~   >>> 我有背景

四:  数字装字符串>>> 1 +  "1"  >>> "11"

    var a= 1  a.tostring() >>> "1"  (也叫构造函数)

    String(a)

  字符串转数字: a = "77878.455"

  Number(a)  >>>转换数字

  parseFloat(a)  >>>转换成浮点数

  parseInt(a)  >> 转换为整数

  var a = 7/0   等同于 Infinity  (表示无穷大)

五 流程控制:

  if else elif,  for in ,  while, break continue, return (python)

  if else  for循环 while break  continue, do-while switch

var age = 18

if (age>18) {

alert("可以去会所了")

} else if (True) {

符合这个条件执行这里

} else {

以上都不符合

}

 

六 逻辑运算 : &&  ,  || ( and , or)

  三元运算    3>2 ? "真" : "假"    >>>  如果3>2 为真选择 "真"  ,否则选择 ""假""

 

switch:   var weather = "晴天";

  switch (weather) {

          case "晴天":

            console.log("今天天气很好");

            break;

          case "雨天":

            console.log("今天天气不好");

            break ;

          case "下雪":

            console.log("今天天气不错");

            break;

          default:

             console.log("以上天气都不好")    

          }

 

七  while :  do  while

var i = 1;

while (i<=10) {

  consolel.log(i)

  i++

}   >>>>0-9

 

 

var a=3;

do {  // 执行语句

  console.log(a}

  a++

  }   while (a<10) //判断循环条件

 

八 for循环

 

var arr = ["ad", "dgdg"]

for(var i = 0;  i<arr.lenth; i++) {

  console.log (arr[i])

}   >>>遍历打印arr数组中的每一个元素

 

forEach:

 arr.forEach  ( function(item, index)  {

   console.log(item, index)

    })

 

九  for循环的嵌套

for (var i = 0; i<6; i++){

  for(var j = 0; j<6; j++) {

    document.write("*")  >>>内循环    

      }

  document.write(""*)  >>>外循环

}

 

十 数组 array

var colors = ["red", "green", "yellow"]

var list =  new Array()    |  var list = new Array("a", "b")

list[0] = "alex"

var newArr = colors.concat(list)   #  将两个数组合并

/  在数组中添加或移除元素  (.splice)
//    删除:第一个参数表示 起始的位置 第二个参数删除的个数
//    添加:第二个参数是0,表示添加
//    arr.splice(0,1,items...)

 

 

十一 :  data  

var data = new Date()

.getDate() 标准时间

getMonth  月份-1

getDAy  星期

tolocaleString()  #转换成本地时间

 

十二 Math

Math.floor(num)  # 取整 3.99>>>3

Math.ceil(0) # 向上取整 , 3.001 >>> 4

Math.random()  # 0-4 之间的小数

x - y  之间的随机数 >>>  Math.random()*(y-x) >>> 0-200    + x >>> 随机数

 

十三: 函数:

function name(形参) {

    执行体

    return xxx

}

for E:  function add(x, y) {

    retrun x+y    }

调用 add(3, 5)

匿名函数:  var add = function() {

       alert(000)   }

 

十四: 伪数组:

  arguments:

for (var i = 0; i < arguments.length; i++){

      console.log(arguments[i])  }

tip: 将数组的元素放到arguments中,  可以继承数组的方法

 

 

十五 DOM 文档对象模型

  利用id或者标签选选取素对象 设置触发动作  执行动作效果 

  <div id="odiv"></div>

  var isTrue = true

  odiv.onclick = function(){

     if(istrue) {odiv.style.background = "green"

      istrue

          }else{ 

      odiv.style.background = "red"

        istrue = false   }    

      }   #   可以循环点击更换颜色

 

posted @ 2018-11-07 19:36  python传言  阅读(169)  评论(0编辑  收藏  举报