1. <!-- 在js中调用函数时传递变量函数时,是值传递还是引用传递
    2. 值传递
    3. 、-->
    4. var a = 3
    5. function fn (a){
    6. a= a+1
    7. }
    8. fn(a)
    9. console.log(a)
    10. function fn2 (obj){
    11. console.log(obj.name)
    12. }
    13. var obj = {name: 'tom'}
    14. fn2(obj)
    1. var btns = document.getElementsByTagName("button");
    2. // 1给每个btn绑定点击事情
    3. for(var i=0;i<btns.length;i++){
    4. btns[i].onclick = function(){
    5. // 2点击对应的元素,将其父元素删除
    6. var isShow = confirm("是否删除");
    7. console.log(isShow);
    8. if(isShow){
    9. this.parentNode.style.display = "none"
    10. }
    11. }
    12. }

    history事件

    1. <button id="btn">高云锋</button>
    2. <script>
    3. var btn =document.getElementById("btn");
    4. btn.onclick = function(){
    5. // 回退到前一个页面
    6. history.back()
    7. }

    screen事件

    1. <!-- screen 测量屏幕的高度和宽度
    2. availWidth 屏幕的宽度
    3. -->
    4. var sw = screen.availWidth;
    5. var swe = screen.availHeight;
    6. console.log(sw)
    7. console.log(swe)
    8. // 可视区域的高度
    9. var vh =document.documentElement.clientHeight;
    10. console.log(vh)
    1. 怎么判断滚动条的距离
    2. var bodyH = document.body.clientHeight;
    3. var vw = document.documentElement.clientHeight
    4. console.log(bodyH);
    5. console.log(vw);
    6. // 滚动条滚动的距离+vw ==bodyH
    7. window.onscroll = function(){
    8. var scrollTop = window.scrollY;
    9. console.log(scrollTop)
    10. }
    11. // scrollTop+vw ==bodyH
    1. //点击键盘的center 获取input的值
    2. var app = document.getElementById("app");
    3. app.onkeydown = function(event){
    4. if(event.keyCode ==13){
    5. console.log(this.value)
    6. //将这个值传递给另一个页面
    7. location.href = "15search.html?s"+this.value;
    8. }
    9. }
    10. <!-- location.href 传递给另一个页面 -->
    11. console.log(location.search)
    12. console.log(decodeURIComponent("%E9%AB%98%E4%BA%91%E9%94%8B"))
    1. // 以直接量的方式创建 tips:在函数前后调用都是可以的
    2. function go(){
    3. console.log("hellow word");
    4. }
    5. //以变量的方式创建 tips:只能在函数之后调用
    6. var b = function(){
    7. console.log("good")
    8. }
    9. go();
    10. b();

    函数的返回值

    1. <!-- 函数的返回值
    2. 函数的执行结果
    3. 函数遇到return语句会立即停止,退出
    4. 3return语句的作用,将函数内容的值返回给外部
    5. -->
    6. <script>
    7. function go(){
    8. return 10;
    9. console.log("hellow word")
    10. }
    11. var res = go();
    12. console.log(res)
    13. </script>

    函数的参数

    1. function go(a,b){
    2. //函数的参数是局部变量
    3. console.log(a);
    4. console.log(b);
    5. }
    6. /*
    7. 伪代码
    8. function go(var a,b){
    9. console.log(a);
    10. consoel.log(b);
    11. }
    12. js传不定参
    13. */
    14. go();
    15. go(10);
    16. go(10,20);

    重载

    1. /*使用function声明的方法,本质上是window*/
    2. // var a = 10;
    3. // var a = 20;
    4. // console.log(a)
    5. function go(a){
    6. console.log(a)
    7. }
    8. function go(a,b){
    9. console.log(a);
    10. console.log(a+b);
    11. }
    12. window.go(10);

    重载

    1. <!--重载: 函数名相同,参数不同,根据传入的参数
    2. js不支持重载,
    3. 就要js支持的话 arguments
    4. js传递不定参,函数使用arguments对象管理函数内部的参数
    5. -->
    6. <script>
    7. function go(){
    8. console.log(arguments)
    9. }
    10. go(10,20,4)
    1. /*要有一个参数和两个参数的场景*/
    2. function go(){
    3. if(arguments.length ==1){
    4. console.log(arguments[0])
    5. }else if(arguments.length ==2){
    6. console.log(arguments[0]);
    7. console.log(arguments[0]+arguments[1])
    8. }
    9. }
    10. go(10,20)
    1. /*
    2. 函数和对象的方法
    3. 函数作为对象的方法
    4. */
    5. var obj ={
    6. name:"html",
    7. sayName:function(){
    8. console.log(this.name)
    9. }
    10. }
    11. // 函数是对象的一个方法
    12. obj.sayName();
    13. // this关键字的指向问题
    14. /*
    15. onclick 谁执行事件,this指向谁
    16. 2谁执行方法,this指向谁
    17. */
    1. 回调函数
    2. /*回调函数
    3. 就是函数作为参数传递给另一个参数
    4. */
    5. function show(a){
    6. console.log(a);
    7. }
    8. function go(callback){
    9. var b = 10;
    10. callback(b)
    11. }
    12. go(show);
    13. /*
    14. callback =show
    15. callback = function(a){
    16. console.log(a);
    17. }
    18. */
    1. function go(callback){
    2. var b =20;
    3. callback(b);
    4. }
    5. go(function(res){
    6. console.log(res)
    7. })
    1. js的回调
    2. /*
    3. 回调函数:就是将函数作为函数传递给另一个函数
    4. 使用场景:异步问题
    5. */
    6. function go(callback){
    7. var b =20;
    8. callback(b);
    9. }
    10. go(function(res){
    11. console.log(res)
    12. })

    es6

    1. var show = function(a){
    2. console.log(a);
    3. }
    4. /*es6 箭头
    5. 特点:省略了function关键字
    6. */
    7. var go=(a)=>{
    8. console.log(a)
    9. }
    10. go(30)
    1. var go = (a)=>{
    2. console.log(a);
    3. }
    4. /*简写
    5. tips:如果参数只有一个可以省略小括号,输出语句只有一段可以省略大括号
    6. */
    7. var show =a=>console.log(a);
    8. show(10);
    1. var go =(a)=>{
    2. return a;
    3. }
    4. /*如果输出语句只有一段return语句,可以省略return;*/
    5. var show =a=>a;
    6. console.log(show(10))
    1. 数组
    2. /*
    3. 数组
    4. 增 删 该 查 遍历
    5. */
    6. //数组增加的方法 使用push来进行增加
    7. var arr = [1,2,3,];
    8. arr.push(4);
    9. arr.push(5,6)
    10. arr.push([7,8,9])
    11. console.log(arr)
    1. <!-- 数组从前面增加
    2. unshift
    3. 1,可以增加多个值
    4. 使用场景 历史搜索,下拉刷新
    5. -->
    6. <script>
    7. var arr = [1,2,3];
    8. arr.unshift(0,-1);
    9. console.log(arr)
    1. <!-- indexOF 可以判断数组中的下标
    2. -1 数组中没有这个值
    3. -->
    4. <script>
    5. var arr = [1,2,3,4];
    6. console.log(arr.indexOf(4))
    7. console.log(arr.indexOf(3))
    8. console.log(arr.indexOf(9))
    1. todoList
    2. var input = document.getElementById("input");
    3. var app = document.getElementById("app")
    4. var historys = []
    5. input.onkeydown = function(event){
    6. if(event.keyCode==13){
    7. //2只要数组中没有的值同时字符不能空,才向数组添加
    8. if(this.value != "" && historys.indexOf(this.value) == -1){
    9. var p = document.createElement("p");
    10. p.innerHTML = this.value;
    11. app.prepend(p);
    12. //console.log
    13. historys.unshift(this.value);
    14. console.log(historys)
    15. }else{
    16. alert("输入的字是空字符或者相同的字段")
    17. }
    18. }
    19. }
    1. /*concat
    2. 特点1 支持添加多个值
    3. 2 支持数组数据的格式
    4. 3 不会改变数组原本的结构
    5. */
    6. var arr = [1,2,3];
    7. var b = arr.concat([4,5]);
    8. console.log(b);
    9. console.log(arr)
    10. var test = [6,7,8];
    11. test.push([4,5]);
    12. console.log(test)