前端笔试面试题收集
来自:http://my.oschina.net/june6502/blog/387721
6 请编写一个JavaScript 函数toRGB,它的作用是转换CSS中常用的颜色编码。 要求:
alert(toRGB("#0000FF")); // 输出 rgb(0, 0, 255)
alert(toRGB("invalid")); // 输出 invalid
alert(toRGB("#G00")); // 输出 #G00
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
function toRGB(color){ var hh = ""; var kk = []; var reg =/^#[0-9a-zA-Z]{3}$/; //设置正则规则 if(color=="invalid") return "invalid";//如果无效返回 if(reg.test(color)) return color; //如果是三为数表示,直接返回。 else {for(var i=1;i<6;i=i+2){ hh =color.substr(i,2);//substr()从第i位开始截取2位字符 var cc =parseInt(hh,16); kk.push(cc); } }return ("RGB("+kk+")");}alert(toRGB("#0000FF")); // 输出 rgb(0, 0, 255)alert(toRGB("invalid")); // 输出 invalidalert(toRGB("#G00")); // 输出 #G00 |
7 尝试实现注释部分的Javascript代码,可在其他任何地方添加更多代码(如不能实现,说明一下不能实现的原因):
var Obj = function(msg){
this.msg = msg;
this.shout = function(){
alert(this.msg);
}
this.waitAndShout = function(){
//隔五秒钟后执行上面的shout方法
}}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var Obj = function(msg){this.msg = msg;var _self=this;var shout=this.shout = function() {alert(_self.msg);_self.waitAndShout();}this.waitAndShout = function() {setTimeout(shout, 5000);}}var testObj = new Obj("Hello,World!");testObj.shout(); |
8 请编写一个JavaScript函数,它的作用是校验输入的字符串是否是一个有效的电子邮件地址。要求: a) 使用正则表达式。 b) 如果有效返回true ,反之为false。
|
1
2
3
4
5
6
7
|
function checkEmail(email){ var stand=/^[0-9a-zA-Z_.-]+@[0-9a-zA-Z_.-]+$/; if(stand.test(email)) return true; else return false;}checkEmail("yolina_379653734@qq.com"); |
11 请编写一段JavaScript脚本生成下面这段DOM结构。要求:使用标准的DOM方法或属性。
<div id=”example”> <p class=”slogan”>淘!你喜欢</p> </div>
|
1
2
3
4
5
6
7
8
9
10
11
|
function createDOM(){ var d=document.createElement("div"); d.setAttribute('id','example'); var p=document.createElement("p"); p.className='slogan'; var pText=document.createTextNode("淘!你喜欢"); p.appendChild(pText); d.appendChild(p); document.body.appendChild(d);}createDOM(); |
12 请用CSS定义p标签,要求实现以下效果: 字体颜色在IE6下为黑色(#000000);IE7下为红色(#ff0000);而其他浏览器下为绿色(#00ff00)。
|
1
2
3
4
5
6
7
8
9
10
11
|
p {color:#0f0;*color:#f00;_color:#000;}* html p{color:#000;}*+html p{color:#f00;}
|
浙公网安备 33010602011771号