web前端開發筆試集錦(javascript篇1)轉

來源:互聯網
上載者:User

標籤:

原文出處: http://hi.baidu.com/jinhui04/item/eaf40034fa00def597f88dbc


1, 判斷字串是否是這樣組成的,第一個必須是字母,後面可以是字母、數字、底線,總長度為5-20var reg = /^[a-zA-Z][a-zA-Z_0-9]{4,19}$/;reg.test("a1a__a1a__a1a__a1a__");

2,截取字串abcdefg的efg

var str = "abcdefg";if (/efg/.test(str)) {var efg = str.substr(str.indexOf("efg"), 3);alert(efg);}

3,判斷一個字串中出現次數最多的字元,統計這個次數

//將字串的字元儲存在一個hash table中,key是字元,value是這個字元出現的次數var str = "abcdefgaddda";var obj = {};for (var i = 0, l = str.length; i &lt; l; i++) {var key = str[i];if (!obj[key]) {obj[key] = 1;} else {obj[key]++;}}/*遍曆這個hash table,擷取value最大的key和value*/var max = -1;var max_key = "";var key;for (key in obj) {if (max < obj[key]) {max = obj[key];max_key = key;}}alert("max:"+max+" max_key:"+max_key);

4,IE與FF指令碼相容性問題

(1) window.event:

表示當前的事件對象,IE有這個對象,FF沒有,FF通過給事件處理函數傳遞事件對象

(2) 擷取事件來源

IE用srcElement擷取事件來源,而FF用target擷取事件來源

(3) 添加,去除事件

IE:element.attachEvent(“onclick”, function) element.detachEvent(“onclick”, function)

FF:element.addEventListener(“click”, function, true) element.removeEventListener(“click”, function, true)

(4) 擷取標籤的自訂屬性

IE:div1.value或div1[“value”]

FF:可用div1.getAttribute(“value”)

(5) document.getElementByName()和document.all[name]

IE;document.getElementByName()和document.all[name]均不能擷取div元素

FF:可以

(6) input.type的屬性

IE:input.type唯讀

FF:input.type可讀寫

(7) innerText textContent outerHTML

IE:支援innerText, outerHTML

FF:支援textContent

(8) 是否可用id代替HTML元素

IE:可以用id來代替HTML元素

FF:不可以

這裡只列出了常見的,還有不少,更多的介紹可以參看JavaScript在IE瀏覽器和Firefox瀏覽器中的差異總結

5,規避javascript多人開發函數重名問題

(1) 可以開發前規定命名規範,根據不同開發人員開發的功能在函數前加首碼

(2) 將每個開發人員的函數封裝到類中,調用的時候就調用類的函數,即使函數重名只要類名不重複就ok

6,javascript物件導向中繼承實現

javascript物件導向中的繼承實現一般都使用到了建構函式和Prototype原型鏈,簡單的代碼如下:

function Animal(name) {this.name = name;}Animal.prototype.getName = function() {alert(this.name)}function Dog() {};Dog.prototype = new Animal("Buddy");Dog.prototype.constructor = Dog;var dog = new Dog();

7,FF下面實現outerHTML

FF不支援outerHTML,要實現outerHTML還需要特殊處理

思路如下:

在頁面中添加一個新的元素A,複製一份需要擷取outerHTML的元素,將這個元素append到新的A中,然後擷取A的innerHTML就可以了。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>擷取outerHMTL</title><style>div{ background:#0000FF;width:100px;height:100px;}span{ background:#00FF00;width:100px;height:100px;}p{ background:#FF0000;width:100px;height:100px;}</style></head><body><div id="a"><span>SPAN</span>DIV</div><span>SPAN</span><p>P</p><script type="text/javascript">function getOuterHTML(id){var el = document.getElementById(id);var newNode = document.createElement("div");document.appendChild(newNode);var clone = el.cloneNode(true);newNode.appendChild(clone);alert(newNode.innerHTML);document.removeChild(newNode);}getOuterHTML("a");</script></body></html>

8,編寫一個方法 求一個字串的位元組長度

假設:

一個英文字元佔用一個位元組,一個中文字元佔用兩個位元組

function GetBytes(str){var len = str.length;var bytes = len;for(var i=0; i<len; i++){if (str.charCodeAt(i) > 255) bytes++;}return bytes;}alert(GetBytes("你好,as"));

9,編寫一個方法 去掉一個數組的重複元素

var arr = [1 ,1 ,2, 3, 3, 2, 1];Array.prototype.unique = function(){var ret = [];var o = {};var len = this.length;for (var i=0; i<len; i++){var v = this[i];if (!o[v]){o[v] = 1;ret.push(v);}}return ret;};alert(arr.unique());

10,寫出3個使用this的典型應用

(1)在html元素事件屬性中使用,如

<input type=”button” onclick=”showInfo(this);” value=”點擊一下”/>

(2)建構函式

function Animal(name, color) {this.name = name;this.color = color;}

(3)

<input type="button" id="text" value="點擊一下" /><script type="text/javascript">var btn = document.getElementById("text");btn.onclick = function() {alert(this.value); //此處的this是按鈕元素}</script>

(4)CSS expression運算式中使用this關鍵字

<table width="100px" height="100px"><tr><td><div style="width:expression(this.parentNode.width);">div element</div></td></tr></table>

12,如何顯示/隱藏一個DOM元素?

el.style.display = "";el.style.display = "none";

el是要操作的DOM元素

13,JavaScript中如何檢測一個變數是一個String類型?請寫出函數實現

String類型有兩種產生方式:

(1)Var str = “hello world”;

(2)Var str2 = new String(“hello world”);

function IsString(str){return (typeof str == "string" || str.constructor == String);}var str = "";alert(IsString(1));alert(IsString(str));alert(IsString(new String(str)));

14,網頁中實現一個計算當年還剩多少時間的倒數計時程式,要求網頁上即時動態顯示“××年還剩××天××時××分××秒”

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>倒計時</title></head><body><input type="text" value="" id="input" size="1000"/><script type="text/javascript">function counter() {var date = new Date();var year = date.getFullYear();var date2 = new Date(year, 12, 31, 23, 59, 59);var time = (date2 - date)/1000;var day =Math.floor(time/(24*60*60))var hour = Math.floor(time%(24*60*60)/(60*60))var minute = Math.floor(time%(24*60*60)%(60*60)/60);var second = Math.floor(time%(24*60*60)%(60*60)%60);var str = year + "年還剩"+day+"天"+hour+"時"+minute+"分"+second+"秒";document.getElementById("input").value = str;}window.setInterval("counter()", 1000);</script></body></html>

15,補充代碼,按一下滑鼠Button1後將Button1移動到Button2的後面<div> <input type=”button” id =”button1″ value=”1″ onclick=”???”> <input type=”button” id =”button2″ value=”2″ /”> </div>

<div><input type="button" id ="button1" value="1" onclick="moveBtn(this);"><input type="button" id ="button2" value="2" /></div><script type="text/javascript">function moveBtn(obj) {var clone = obj.cloneNode(true);var parent = obj.parentNode;parent.appendChild(clone);parent.removeChild(obj);}</script>

16,JavaScript有哪幾種資料類型

簡單:Number,Boolean,String,Null,Undefined

複合:Object,Array,Function

17,下面css標籤在JavaScript中調用應如何拼字,border-left-color,-moz-viewport

borderLeftColor

mozViewport

18,JavaScript中如何對一個對象進行深度clone

function cloneObject(o) {if(!o || ‘object‘ !== typeof o) {return o;}var c = ‘function‘ === typeof o.pop ? [] : {};var p, v;for(p in o) {if(o.hasOwnProperty(p)) {v = o[p];if(v && ‘object‘ === typeof v) {c[p] = Ext.ux.clone(v);}else {c[p] = v;}}}return c;};

19,如何控制alert中的換行

\n alert(“p\np”);

20,請實現,滑鼠點擊頁面中的任意標籤,alert該標籤的名稱.(注意相容性)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>滑鼠點擊頁面中的任意標籤,alert該標籤的名稱</title><style>div{ background:#0000FF;width:100px;height:100px;}span{ background:#00FF00;width:100px;height:100px;}p{ background:#FF0000;width:100px;height:100px;}</style><script type="text/javascript">document.onclick = function(evt){var e = window.event || evt;var tag = e["target"] || e["srcElement"];alert(tag.tagName);};</script></head><body><div id="div"><span>SPAN</span>DIV</div><span>SPAN</span><p>P</p></body></html>

web前端開發筆試集錦(javascript篇1)轉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.