zoukankan      html  css  js  c++  java
  • js小点总结

    1. 事件代理:
       事件代理利用了时间冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件。

    function getEventTarget(e) {
      e = e || window.event;
      return e.target || e.srcElement;
    }

    2. 解释下js中this是如何工作的。

         1) this永远指向函数运行时所在的对象,而不是函数被创建时所在的对象。匿名函数或不处于任何对象的函数指向window
         2)call,apply指定this是谁就是谁
         3)普通的函数调用,函数被谁调用this就是谁
    3. 未定义的或者定义了未被赋值的为 undefined,null是特殊的object是关键字表明变量没有有效的值,NaN是特殊的number
        运算时null与undefined都可以被类型转换为false,但不等值于false:

    typeof NaN == "number"
    typeof null == "object"
    typeof undefined == "undefined"
    null与undefined相等,NaN不与任何相等与他自己也不相等
    null == undefined true
    null == null true
    undefined == undefined true
    NaN == NaN false
    NaN == false false
    null == false false
    isNaN(x)

    4. JS定时函数

        var interv = setInterval(function,200);  clearInterval(interv ); 循环执行直到被清除
        setTimeout(function,200); 延迟多长时间执行
    5. localStorage, sessionStorage和cookie的区别?
        1)localStorage和sessionStorage是html5的web storage api提供的,有getItem、setItem、removeItem、clear等方法,
    提供更大容量的存储
        2)sessionStorage是会话级别的存储,结束会话时数据消失,localStorage用于持久化本地存储
        3)cookie大小首先,可与服务器进行交互,没有提供存取值的方法。
    6. JS全局属性方法
        Infinity 正负无穷大
        NaN 非数字
        undefined 没有定义的或者未赋值的
        isNaN() 判断是不是Number类型
        Number() 转换为数字
        Boolean() 0,"",null,undefined false
        String() 转换成字符串
        parseInt()
        parseFloat()
    7. BOM
        window: 全局变量是window对象的属性,全局方法是window对象的方法,HTML的document也是window对象的属性
        navigator object: 记载一些关于浏览器的信息。navigator.userAgent
        screen object: 当前屏幕的宽高
        history object: history.forward();history.back();
        location object: 获取当前页面URL,{"hostname":"","pathname":"","port":"","protocol":""}
        var w=window.innerWidth|| document.documentElement.clientWidth|| document.body.clientWidth;
        var h=window.innerHeight|| document.documentElement.clientHeight|| document.body.clientHeight;
    8. JS操作数组的方法:

    concat() 连接两个或更多的数组,并返回结果
    sort() 对数组的元素进行排序
    reverse() 颠倒数组中元素的顺序
    join() 把数组中所有元素放入一个字符串,元素通过指定的分隔符进行分隔
    toString() 把数组转换为字符串,并返回结果
    pop() 删除并返回数组的最后一个元素
    push() 向数组的末尾添加一个或更多的元素,并返回新的长度
    shift() 删除并返回数组的第一个元素
    unshift() 想数组的开头添加一个或更多的元素,并返回新的长度
    slice() 从某个已有的数组返回指定的元素
    splice() 删除元素,并向数组添加新元素

    9. JS操作字符串的方法:

    concat()    连接字符串
    indexOf()    检索字符串
    lastIndexOf()    从后向前搜索字符串
    match() 找到一个或多个正则表达式的匹配数组
    search() 检索与正则表达式匹配的值,返回位置
    replace() 替换与正则表达式匹配的字串
    split() 把字符串分割为字符串数组
    slice() 提取字符串的片段,并在新的字符串中返回被提取的部分
    substr() 从起始索引号提取字符串中指定数组的字符
    substring() 提取字符串中指定的索引号之间的字符

    10. js操作DOM

    document.getElementById();
    document.getElementsByTagName();
    document.getElementsByClassName();
    document.getElementById().attribute = val;
    document.getElementById().style.property = style;
    

    13. 跨域访问解决办法:

       1)Chrome浏览器解决方法: 在chrome快捷目标后加上 --args --disable-web-securitys
       2)iframe里面都加上document.domain = "aa.com"
       3)JS文件注入,请求是使用<script>标签来请求的
       4)jquery ajax支持get请求的跨域jsonp方式
            a.com/a.jsp         

    function test(data){alert(data);}
    <Script src="http://www.b.com/index!getData.action?jsoncallback=test"></script>
    b.com/b.jsp
    $(param.jsoncallback)({"name":"11","age":11});
    客户端接收的response
    test({"name":"11","age":11});
    5)使用html5的配置http头协议

        服务器设置

    header('Access-Control-Allow-Origin:http://www.a.com');
    header('Access-Control-Allow-Methods:POST,GET');
    header('Access-Control-Allow-Credentials:true');
    echo 'Cross-domain Ajax';

       前端发送请求

    var xhr = new XMLHttpRequest(); ;
    xhr.open('GET', 'http: //b.com/cros/ajax.php', true);
    xhr.withCredentials = true;
    xhr.onload = function () { 
    alert(xhr.response);//reposHTML;
    }; 
    xhr.onerror = function () {
    alert('error making the request.');
    };
    xhr.send();

    14. Ajax   

    $.ajax({
           url: 'index.php?route=profile/favorite/loadMore',
           type: 'post',
           data: '',
           dataType: 'json',
           success: function(json) {}
       });

    15. jQuery操作属性
        hasClass(), addClass(), removeClass(), toggleClass(), attr(), removeAttr(), val()
        $("input[name='name'][value='name1']").prop("checked",true)

    16.  解码字符串

           escape(string)返回已编码的字符串的副本, 不会对ASCII字母,数字和标点符号 - _ . ! ~ * ' ( )进行编码。其他所有的字符都会被转移序列替换。
           encodeURI(URIstring)参数作为URI,返回URIString的副本, 不会对ASCII字母,数字和标点符号 - _ . ! ~ * ' ( )进行编码。不会对URI中具有特殊含义的ASCII变电符号;/?:@&=+$,#转移
           encodeURIComponent(URIstring)参数作为URI的一部分, 不会对ASCII字母,数字和标点符号 - _ . ! ~ * ' ( )进行编码。其他字符;/?:@&=+$,#由一个或多个十六进制的转义序列替换
           escape()除了ASCII字母,数字和特定的符号外,对传进来的字符串全部进行转义编码,所以对URI进行编码用encodeURI(),此方法对URI中的合法字符串不会被编码。encodeURIComponent()在编码单个URIComponent()它可以将参数中的中文,特殊字符进行转移而不会影响整个URL

    17. 各种类型转换

          数字转换成字符串:("" +) > String() > .toString() > new String()

          浮点转化成整型:Math.floor()或者Math.round()

          字符串转化为数字:parseInt()

            var myVar = "3.14159",
            str = "" + myVar, //  to string  
            i_int = ~ ~myVar,  //  to integer  
            f_float = 1 * myVar,  //  to float  
            b_bool = !!myVar,  /*  to boolean - any string with length  and any number except 0 are true */
            array = [myVar];  //  to array  
  • 相关阅读:
    使用pod install 出现bad interpreter: No such file or directory
    简单易用且功能丰富的纯Swift下载框架
    Swift主题色顶级解决方案一
    如何基于WKWebView开发一个功能完善的资讯内容页
    关于iPhone X 的适配
    iOS11及Xcode9适配问题汇总
    优豆云
    Mac 网站屏蔽修改
    c语言
    iOS 12 前台通知shouldAlwaysAlertWhileAppIsForeground崩溃问题
  • 原文地址:https://www.cnblogs.com/lindsayzhao103011/p/3764741.html
Copyright © 2011-2022 走看看