網上面試資料整理

整理網上的面試題html

1、去空格jquery

去除全部空格: str = str.replace(/\s*/g,"");      

去除兩頭空格: str = str.replace(/^\s*|\s*$/g,"");

去除左空格: str = str.replace( /^\s*/, 「」);

去除右空格: str = str.replace(/(\s*$)/g, "");
--------------------- 
做者:wdlhao 
來源:CSDN 
原文:https://blog.csdn.net/wdlhao/article/details/79079660 
版權聲明:本文爲博主原創文章,轉載請附上博文連接!

 

2、獲取url中傳入的參數面試

<script>
        // 如何獲取瀏覽器URL中查詢字符串中的參數?
        // 測試地址爲:http://www.runoob.com/jquery/misc-trim.html?channelid=12333&name=xiaoming&age=23
        
        function showWindowHref(){
            // var sHref = window.location.href;
            // 沒法直接獲取測試地址,直接如下面的字符串操做
            var sHref= 'http://www.runoob.com/jquery/misc-trim.html?channelid=12333&name=xiaoming&age=23;'
            var args = sHref.split('?');
            // console.log(args)
            var arrs = args[1].split('&')
            // console.log(arrs)
            var obj = {};
            for(let i = 0 , len = arrs.length ; i < len ; i++){
                var arr = arrs[i].split('=')
                console.log(arr)
                obj[arr[0]] = arr[1];
            }
            return obj;
        }

        showWindowHref()
        /* 
            (2) ["channelid", "12333"]
            (2) ["name", "xiaoming"]
            (2) ["age", "23;"]
        */ 

    </script>
--------------------- 
做者:wdlhao 
來源:CSDN 
原文:https://blog.csdn.net/wdlhao/article/details/79079660 
版權聲明:本文爲博主原創文章,轉載請附上博文連接!
 

 

3、this的應用數組

  一、普通函數,構造函數,箭頭函數中this指向瀏覽器

 

<body>
<input type="button" id="text" value="點擊一下" />
</body>

 

<script>
// this的指向問題 // 普通函數,誰調用指向誰 function fn(){ console.log(this); } fn(); //Window // 構造函數,指向實例化的對象 function Fn(){ console.log(this); } new Fn(); //Fn {} // // 箭頭函數 document.onclick = function(){ console.log(this) } //#document document.onclick = ()=>{ console.log(this) } //Window var btn = document.getElementById("text"); btn.onclick = function() { alert(this.value); //此處的this是按鈕元素 }
</script>

   二、apply 和 call 求數組最大值app

    // 數組求最大值
        var arr1 = [55,66,33,45,61,99,38]
            // apply
        console.log(Math.max.apply(this,arr1))    //99
            // call
        console.log(Math.max.call(this,arr1))    //NAN
        console.log(Math.max.call(this,55,66,33,45,61,99,38))    //99

  總結:函數

一、構造函數的this指向實例化後的那個對象
二、普通函數的this指向調用該函數的那個對象
三、箭頭函數的this指向建立時的那個對象,而不是引用時的那個對象

四、經過apply,call能夠求數組中的最大值
    語法以下:
       Math.max.apply(this,arr)

 

 

 

 

 

 

 

若有問題,請與本人聯繫,當即刪除測試

相關文章
相關標籤/搜索