zoukankan      html  css  js  c++  java
  • js随笔小技巧

    1.根据条件来加载js文件

    <script>!window.jQuery && document.write('<script src="http://code.jquery.com/jquery-1.4.2.js"><\/script>');</script>

    2.json字符串转换为对象的另一个方法

    var json='{"name":"CJ","age":18}';
    var data =(new Function("","return "+json))();
    
    //显式创建对象的方法
    var add = new Function("x", "y", "return(x+y)");

    3.滑动滚动条动态加载数据--瀑布流

    window.onscroll = function(){
        var de = document.documentElement;
        if(Element.offset().x<de.scrollTop+de.clientHeight){
            //加载数据
        }
    }

    4.终止正在发送的http请求

    IE:window.document.execCommand("Stop") 
    W3C:window.stop();

    5.设置浏览器为可编辑模式

    window.document.designMode = "on";
    window.document.contentEditable=true;
    window.document.execCommand('InsertImage',false,'http://localhost/phpmyadmin/themes/original/img/logo_right.png');

    6.js匹配中文正则:

    var reg =/^[\u4e00-\u9fa5]+$/;

    8.进制之间转换

    
            parseInt(num, radix);
            radix 描述num的进制值  默认0x为16进制 0为8进制  其他为10进制
            
            //将111做为2进制来转换,(当遇到第一个不符合进制要求的字符时会面的不在考虑),从左至右只将符合二进制数的进行转换 
            var a='11160';
            alert(parseInt(a,2));
            
            *.toString(radix); //要转成的进制值
            
            var a = "从".charCodeAt(0);
            alert(a);
    
            var b = String.fromCharCode("20174");
            alert(b);
            
            //根据unicode码返回字符串
            var test = String.fromCharCode(112, 108, 97, 105, 110);

     9、快速交换字符串中两个单词的位置

    var str = "hello world";
    console.log(str.replace(/(\S+)(\s+)(\S+)/, "$3$2$1"));

    10、jQuery.ajax在gbk页面传中文乱码

    contentType: "application/x-www-form-urlencoded; charset=utf-8",
    #http://blog.csdn.net/maklonzjing/article/details/3923222

    11、callee、caller

    #函数自身
    arguments.callee
    
    #调用函数者
    arguments.callee.caller

     12、下面的script标签会等上面的引入的js文件加载完成后才会被执行

           外部引入的js会阻塞下面页面组件的加载 如 img等

     13、js是不能直接比较数组是否相等的,可以调用数组的toString后在做比较

    var a = [1,2,3];
    var b = [1,2,3];
    alert(a == b); // false
    //结果是 false. 证明两个数组不能直接比较相等。

     14、判断一个对象是否是jquery对象  

    obj instanceof jQuery

     15、用ajax异步清除浏览器缓存

     16、设置浏览器为可编辑模式,js做编辑器时会用到

    //设置浏览器为可编辑模式
    window.document.designMode = "on";
    window.document.contentEditable=true;
    window.document.execCommand('InsertImage',false,'http://localhost/phpmyadmin/themes/original/img/logo_right.png');

    17、isPrototypeOf instanceof 是等价的 

    //可以这样认为
    Object.prototype.instanceOf = function( iface )
    {
     return iface.prototype.isPrototypeOf( this );
    };

    http://stackoverflow.com/questions/2464426/whats-the-difference-between-isprototypeof-and-instanceof-in-javascript

     18、js数据类型

    Boolean Number String undefined null object(array,function 除基本类型外其他都属于对象类型)

    10.简单的区分ie和现代浏览器

         var ie = -[1,];
         // IE下为NaN,NaN为number类型,转换为boolean则为false
         if(ie){
           alert("not ie");
          }else{
           alert("ie");
         }

  • 相关阅读:
    LeetCode 275. H-Index II
    LeetCode 274. H-Index
    LeetCode Gray Code
    LeetCode 260. Single Number III
    LeetCode Word Pattern
    LeetCode Nim Game
    LeetCode 128. Longest Consecutive Sequence
    LeetCode 208. Implement Trie (Prefix Tree)
    LeetCode 130. Surrounded Regions
    LeetCode 200. Number of Islands
  • 原文地址:https://www.cnblogs.com/siqi/p/2771211.html
Copyright © 2011-2022 走看看