百度面试题

来源:互联网 发布:基三捏脸数据正太 编辑:程序博客网 时间:2024/05/08 20:43
第一题编写一个方法 求一个字符串的字节长度<script language="javascript" type="text/javascript">new function(s){if(!arguments.length||!s) return null;if(""==s) return 0;var l=0;for(var i=0;i<s.length;i++){if(s.charCodeAt(i)>255) l+=2;else l++;}alert(l);}("hello你好,我好,大家好!world!");</script>第二题如何控制alert中的换行<script language="javascript" type="text/javascript">alert("hello/nworld");</script>第三题解释document.getElementById("ElementID").style.fontSize="1.5em"em是个相对单位。第四题将一个类似图中的效果分离成css和html第五题按照格式 xxxx年xx月xx日xx时xx分xx秒动态显示时间 要求不足10的补0<script language="javascript" type="text/javascript">new function(){with(new Date()){var t=function(a){return a<10?"0"+a:a;}alert(getFullYear()+"年"+t(getMonth()+1)+"月"+t(getDate())+"日"+t(getHours())+"时"+t(getMinutes())+"分"+t(getSeconds())+"秒");}}</script>第六题编写一个方法 去掉一个数组的重复元素<script language="javascript" type="text/javascript">Array.prototype.strip=function(){if(this.length<2) return [this[0]]||[];var arr=[];for(var i=0;i<this.length;i++){arr.push(this.splice(i--,1));for(var j=0;j<this.length;j++){if(this[j]==arr[arr.length-1]){this.splice(j--,1);}}}return arr;}var arr=["abc",85,"abc",85,8,8,1,2,5,4,7,8];alert(arr.strip());</script>第七题说出3条以上ff和ie的脚本兼容问题__defineGetter__ __defineSetter__ __proto__同意winter的,另:IE有children,FF没有;IE有parentElement,FF没有;IE有innerText,outerText,outerHTML,FF没有;FF有HTMLElement,HTMLDivElement,XMLDocument,DocumentFragment,Node,Event,Element等等,IE没有;IE有数据岛,FF没有;IE跟FF创建HttpRequest实例的方法不一样。。等等。。第八题按要求写一个简单的ajax示例 简单的没意义 就不写了<html><head><title>loading</title></head><body><div id="load">数据正在加载......</div><script type="text/javascript">var Browser={/**Browser对象用于检测浏览器,其中用到了IE的条件编译*//*@cc_onisIE:true,@*/isFF:window.navigator.appName.toUpperCase().indexOf("NETSCAPE")!=-1?true:false,isOpera:window.navigator.appName.toUpperCase().indexOf("OPERA")!=-1?true:false};Function.prototype.bind=function(object){var _this=this;return function(){_this.apply(object,arguments);}}function HttpRequest(){this.async=true;this.cache=false;this.xmlhttp=function(){if(Browser.isFF&&window.XMLHttpRequest){try{return new XMLHttpRequest();}catch(e){}}else if(Browser.isIE&&window.ActiveXObject){var Version = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP.2.6","Msxml2.XMLHTTP","Microsoft.XMLHTTP.1.0","Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];for(var i=0;i<Version.length;i++){try{return new ActiveXObject(Version[i]);}catch(e){}}}}()||false;}HttpRequest.prototype={send:function(object,url,callback){if(!this.xmlhttp) return;this.xmlhttp.open(object?"post":"get",url,!!this.async);if(object) this.xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded");if(!this.cache){this.xmlhttp.setRequestHeader("No-Cache","1");this.xmlhttp.setRequestHeader("Pragma","no-cache");this.xmlhttp.setRequestHeader("Cache-Control","no-cache");this.xmlhttp.setRequestHeader("Expire","0");this.xmlhttp.setRequestHeader("Last-Modified","Wed, 1 Jan 1997 00:00:00 GMT");this.xmlhttp.setRequestHeader("If-Modified-Since","-1");}if(!this.callback) this.callback=callback;if(!this.async){if(typeof(this.callback)=="string"){eval(this.callback);}else if(typeof(this.callback)=="function"){this.callback(this.xmlhttp);}}else{this.xmlhttp.onreadystatechange=function(){if(this.xmlhttp.readyState==4){if(this.xmlhttp.status==0||this.xmlhttp.status==200){if(typeof(this.callback)=="string"){eval(this.callback);}else if(typeof(this.callback)=="function"){this.callback(this.xmlhttp);}}}}.bind(this);}this.xmlhttp.send(object);},abort:function(){if(this.xmlhttp&&this.xmlhttp.abort) this.xmlhttp.abort();}};//ajax类定义结束new HttpRequest().send(null,"http://bbs.51js.com/index.php",function(r){        document.getElementById("load").innerHTML=r.responseText.match(/<img.*?(?://)?>/img).join("");});</script></body></html>
原创粉丝点击