一、W3C標(biāo)準(zhǔn)有哪些
1耸别、什么是DOCTYPE
DOCTYPE是document type(文檔類型)的簡寫健芭,用來說明你用的XHTML或者HTML是什么版本。
2秀姐、所有標(biāo)簽的元素和屬性的名字都必須使用小寫
3慈迈、所有的屬性必須用引號""括起來
4、把所有<和&特殊符號用編碼表示
5省有、所有的標(biāo)記都必須要有一個相應(yīng)的結(jié)束標(biāo)記
6吩翻、所有的標(biāo)記都必須合理嵌套
7、圖片添加有意義的alt屬性
8锥咸、在form表單中增加lable,以增加用戶友好度
二细移、 js面向?qū)ο蟮膸追N方式搏予。
第一種模式:工廠方式
var lev = function(){
return "腳本之家"
}
function Parent() {
var Child = {};
Child.name = "腳本";
Child.age = 4;
Child.lev = lev;
return Child;
}
let x= Parent()
第二種模式:構(gòu)造函數(shù)
var lev = function(){
return "腳本之家"
}
function Parent() {
this.name = "腳本";
this.age = 4;
this.lev = lev;
}
var x = new Parent()
第三種模式:原型模式
var lev=function(){ return "腳本之家"; };
function Parent(){ };
Parent.prototype.name="李小龍";
Parent.prototype.age="30";
Parent.prototype.lev=lev;
var x =new Parent();
alert(x.name);
alert(x.lev());
第四種模式:混合的構(gòu)造函數(shù),原型方式(推薦)
function Parent(){ this.name="腳本"; this.age=4; };
Parent.prototype.lev=function(){ return this.name; };
var x =new Parent();
alert(x.lev());
第五種模式:動態(tài)原型方式
function Parent() {
this.name = "腳本";
this.age = 4;
if (typeof Parent._lev == "undefined") {
Parent.prototype.lev = function () {
return this.name;
}
Parent._lev = true;
}
};
var x = new Parent();
alert(x.lev());
三弧轧、如何居中DIV雪侥?如何居中一個浮動元素?如何讓絕對定位的div居中精绎?
div{
width: 100px;
height: 100px;
background-color: red;
}
.box1{
/*方法一 */
margin: auto;
/* 方法二 */
position: relative;
left: 50%;
top:50%;
transform: translate(-50%,-50%);
}
/* html*/
<div class="box1"></div>
居中浮動元素
body{
display: flex;
justify-content: center;
align-items: center;
height: 500px;
width: 100%;
}
.box1{
float: right;
}
/*或者在外層套一個盒子速缨,讓這個盒子居中*/
絕對定位的盒子居中
body{
height: 500px;
width: 100%;
position: relative;
}
.box1{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
四、ajax是什么 代乃?如何創(chuàng)建一個ajax旬牲?
ajax 全稱是asychronous javascript and xml就是在瀏覽器不刷新頁面的情況下仿粹,偷偷想服務(wù)器傳遞帶有參數(shù)的http請求,服務(wù)器悄悄回應(yīng)原茅,局部刷新頁面
簡單來說就是調(diào)接口拿數(shù)據(jù)
ajax過程:
創(chuàng)建 XMLHttpRequest 對象,也就是創(chuàng)建一個異步調(diào)用對象
創(chuàng)建一個新的 HTTP 請求,并指定該 HTTP 請求的方法吭历、URL 及驗證信息
設(shè)置響應(yīng) HTTP 請求狀態(tài)變化的函數(shù)
發(fā)送 HTTP 請求
獲取異步調(diào)用返回的數(shù)據(jù)
使用 JavaScript和 DOM 實現(xiàn)局部刷新
var xhr = null;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest()
}else{
xhr =new ActiveXObject("Microsoft.XMLHTTP")
}
xhr.onreadystatechange =function(){
if(xhr.readyState == 4 && xhr.status == 200){
document.getElementById("myDiv").innerHTML = xhr.responseText;
}
}
xhr.open("get","test.txt",true);
xhr.send(null)
五、call()和apply()的含義和區(qū)別
call和apply以及bind 都是用來修改函數(shù)中this的指向問題擂橘;
call和apply都可以讓函數(shù)對象被調(diào)用,兩者的第一個參數(shù)都是對象其他參數(shù)晌区, call和bind要傳參需用逗號分隔,而apply要把所有參數(shù)寫在數(shù)組里通贞,即使只有一個
而bind只會改變this指向朗若,不會立即調(diào)用函數(shù)
六、一個頁面從URL到加載顯示完成昌罩,都發(fā)成了什么
- 瀏覽器先查看緩存中是否存在哭懈,有則直接渲染,沒有進(jìn)行下一步
- 解析域名(DNS解析)峡迷,獲取對應(yīng)的IP地址
- 瀏覽器向服務(wù)器發(fā)起tcp連接银伟,與瀏覽器建立tcp三次握手。
- 握手成功后绘搞,瀏覽器向服務(wù)器發(fā)送http請求彤避,請求數(shù)據(jù)包。
- 服務(wù)器處理收到的請求夯辖,將數(shù)據(jù)返回至瀏覽器
- 瀏覽器收到HTTP響應(yīng)
- 讀取頁面內(nèi)容琉预,瀏覽器渲染,解析html源碼
- 生成Dom樹蒿褂、解析css樣式圆米、js交互
- 客戶端和服務(wù)器交互
- ajax查詢
七、去掉一個數(shù)組中的重復(fù)項啄栓,并按升序排序
let arr= [1,1,1,2,3,6,7,15,5,6]
let s = new Set(arr)
var newArr= [...s]
console.log(newArr);
newArr.sort(function(a,b){
if(a>b){
return 1
}else if(a<b){
return -1
}else{
return 0
}
})
console.log(newArr);
八娄帖、給定一個字符串,每隔n個字符就用逗號隔開
var str = 'shuabvauhfuhfuefnalskdnclkas';
function split(str,num){
if(num<=1){
return false;
}
let result = ''
for (let i = 0; i < str.length; i++) {
if(i%num == 0){
result += str.substring(i,i+num)+","
}
}
return result.substring(0,result.length-1)
}
console.log(split(str,3));
九昙楚、用js實現(xiàn)隨機選取10-100之間的10個數(shù)字近速,存入一個數(shù)組,并排序
var arr=[]
for (let i = 0; i < 10; i++) {
arr.push(Math.floor(Math.random()*90+10))
}
arr.sort(function(a,b){
if(a<b){
return -1
}else if(a>b){
return 1
}else{
return 0
}
})
console.log(arr);
十堪旧、把兩個數(shù)組合并削葱,并刪除第二個元素
var arr1 = [1,2,3,5,81]
var arr2 = [5,6,8,1,5,3]
arr1.push(...arr2)
arr1.splice(1,1)
console.log(arr1);