整理网上的面试题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)
若有问题,请与本人联系,当即删除测试