码迷,mamicode.com
首页 > Web开发 > 详细

js 2017

时间:2017-08-16 12:26:27      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:prot   对象   oct   filter   struct   html   com   char   grep   

JS面向对象

技术分享
<script>
  function num(val) {
    return val * 8
  }
  function Index(name, age) {
    this.name = name;
    this.age = age;
  }
  Index.prototype = {
    constructor: Index
  }
  Index.prototype.test = function () {
    console.log(num(this.age));
  }
  Index.prototype.testB = function () {
    console.log(num(this.name));
  }

  var a = new Index(‘kang‘, 22)
  a.test()
  a.testB()
</script>
View Code

 自调用函数 

技术分享
<script>
  !function (a, b) {
    console.log(a + b)
  }(1, 2);

  (function (c, d) {
    console.log(c + d)
  })(3, 4)
  // 你甚至可以在function前面加一元操作符号
  !function () { /* code */ } ();
  ~function () { /* code */ } ();
  -function () { /* code */ } ();
  +function () { /* code */ } ();
</script>
View Code

 jq $.get(‘a.json‘) 读取json  $.grep()过滤json

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<select name="province" id="province" class="common-select"></select>
<select name="province" id="city" class="common-select"></select>
<button>btn</button>
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script>
  $.get(area_list.json, function (res) {
    // 省份
    var provinceList = $.grep(res, function (n) {
      return n.value.length == 2
    })
    // 初始化城市列表
    var beijingList = $.grep(res,function (n) {
      return n.parent == 11
    })

    function addData(id,dataFilter){
      var dataList = ‘‘
      for(var i = 0; i<dataFilter.length;i++){
        dataList+= <option value="+dataFilter[i].value+">+dataFilter[i].name+</option>
      }
      $("#"+id).append(dataList)
    }
    addData("province",provinceList)  //添加省份
    addData("city",beijingList)  // 添加城市

    // 省份改变后获取新的城市列表
    $("#province").change(function () {
      var cityId = $(this).val();
      var cityList = $.grep(res,function (n) {
        return n.parent == cityId
      })
      $("#city").empty()
      addData("city",cityList) // 添加对应城市
    })
  }, json)

$("button").click(function () {
  console.log($("#province").val());
  console.log($("#city").val());
})

</script>
</body>
</html>
View Code

 

js 2017

标签:prot   对象   oct   filter   struct   html   com   char   grep   

原文地址:http://www.cnblogs.com/gyz418/p/7372702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!