1、有这样一个URL:http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx&e,请写一段JS程序提取URL中的各个GET参数(参数名和参数个数不确定),将其按key-value形式返回到一个json结构中,如{a:'1', b:'2', c:'', d:'xxx', e:undefined}。
答案:
function getData(url){
var result={};
var params=url.split("?")[1].split("&");
for(var i = 0, length = params.length; i < length; i++){
if (/=/.test(params[i])) {
var temp=params[i].split("=");
result[temp[0]]=temp[1];
}else{
result[params[i]]=undefined;
}
}
return result;
}
console.log(getData("http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx&e"));
2、请编写一个JavaScript函数,它的作用是校验输入的字符串是否是一个有效的电子邮件地址。
要求:
a) 使用正则表达式。
b) 如果有效返回true ,反之为false。
function checkEmail(email){
var reg=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
return reg.test(email)?true:false;
}
console.log(checkEmail("liyangbaidm@163.cn.com"));
3、[不定项选择题]按照CommonJS规范,在任何模块代码的作用域下内置了以下哪些变量?
A、module
B、context
C、require
D、exports
答案:ACD?有人知道吗?反正我没学过,还望知道的大神们解答!!!
4、请把以下用于连接字符串的JavaScript代码修改为更有效率的方式
var htmlString = "<div class=”container”>" + "<ul id=”news-list”>";
for (var i = 0; i < NEWS.length; i++) {
htmlString += "<li><a href=”"+NEWS[i].LINK+">"+NEWS[i].TITLE + "</a></li>";
}
htmlString += "</ul></div>";
答案:
var htmlString = "<div class=”container”>" + "<ul id=”news-list”>";
var tempHtm=null;
for (var i = 0,length=NEWS.length; i < length; i++) {
var newsItem=NEWS[i];
tempHtml+="<li><a href=”"+newsItem.LINK+">"+newsItem.TITLE + "</a></li>";
}
htmlString+=tempHtml;
htmlString+="</ul></div>";
说明:JS赋值效率问题
5、请使用原生js实现一个div可拖拽,需要考虑浏览器兼容性。
答案:
function drag(obj) {
obj.onmousedown = function(e) {
var e = e || window.event;
var X = e.clientX - this.offsetLeft;
var Y = e.clientY - this.offsetTop;
if ( obj.setCapture ) {
obj.setCapture();
}
document.onmousemove = function(e) {
var e = e || window.event;
obj.style.left = e.clientX - X + 'px';
obj.style.top = e.clientY - Y + 'px';
}
document.onmouseup = function() {
document.onmousemove = document.onmouseup = null;//释放全局捕获
if ( obj.releaseCapture ) {
obj.releaseCapture();
}
}
return false;
}
}
说明:
var e = e || window.event //兼容W3C和IE
var X=event.clientX-whole.offsetLeft; //获取当前框内点击点对元素左边的最短距离
var Y=event.clicntY-whole.offsetTop; //获取当前框内点击点对元素上边的最短距离
obj.style.left = e.clientX - X + 'px'; //鼠标的横坐标-X得到当前元素的左边距
obj.style.top = e.clientY - Y + 'px'; //鼠标的纵坐标-Y得到当前元素的上边距
document.onmousemove = document.onmouseup = null;//当鼠标松开时,将元素固定在当前位置,取消document的onmousemove事件
obj.setCapture(); //兼容浏览器??
6、用JavaScript语言实现 sort 排序函数,要求:sort([5, 100, 6, 3, -12]) // 返回 [-12, 3, 5, 6, 100]。
如果你有多种解法,请阐述各种解法的思路及优缺点。(仅需用代码实现一种解法,其它解法用文字阐述思路即可)
答案:①利用JS内置的sort函数,速度较快,推荐!
function sort(arr){
return arr.sort(function(item1,item2){
return item1-item2;
})
}
console.log(sort([5, 100, 6, 3, -12]));
②冒泡排序:双重for循环消耗新能太大,不推荐!
7、如何判断浏览器是IE还是火狐,用ajax实现。
答案:
var explorer =navigator.userAgent ;
if (explorer.indexOf("MSIE") >= 0) { //ie
alert("ie");
}else if (explorer.indexOf("Firefox") >= 0) { //firefox
alert("Firefox");
}else if(explorer.indexOf("Chrome") >= 0){ //Chrome
alert("Chrome");
}else if(explorer.indexOf("Opera") >= 0){ //Opera
alert("Opera");
}else if(explorer.indexOf("Safari") >= 0){ //Safari
alert("Safari");
} else if(explorer.indexOf("Netscape")>= 0) { //Netscape
alert('Netscape');
}
这里用到navigator.userAgent,indexOf("MSIE") >= 0进行判断,当然也可以用navigator.userAgent.indexOf("MSIE") != -1 进行判断。