原生JS常用API整理

Anchor 對象:指HTML超鏈接

1.修改一個(gè)鏈接的文本,鏈接和target
//html部分
<a id="myAnchor" >訪問 Microsoft</a>
<input type="button" onclick="changeLink()" value="改變鏈接">
<p>改變超鏈接的文本和 URL。也改變 target 屬性。target 屬性的默認(rèn)設(shè)置是 "_self",這意味著會(huì)在相同的窗口中打開鏈接。通過把 target 屬性設(shè)置為 "_blank",鏈接將在新窗口中打開。</p>

//js部分
function changeLink(){
document.getElementById('myAnchor').innerHTML="訪問 W3School"
document.getElementById('myAnchor').
document.getElementById('myAnchor').target="_blank"
}

Document 對象:指 頁面文檔文本

1.document輸出流輸出 文本 或者 HTML
document.write("Hello World!") //Hello World
document.write("<h1>Hello World!</h1>")  //帶h1標(biāo)簽效果的 Hello World
document.write(document.title) //返回當(dāng)前頁面的標(biāo)題
document.write(document.URL) //返回當(dāng)前頁面的URL
document.URL , document.referrer , location.href 的區(qū)別:

①. 從輸出結(jié)果上,document.URL 和 windows.location.href 沒有區(qū)別。
②. 非要說區(qū)別的話,你只可以讀取document.URL的值,不能修改它。windows.location.href的值你即可以讀取也可以修改,可以使用它進(jìn)行頁面跳轉(zhuǎn)。
③. referrer則是返回前一個(gè)來源頁面的URL,并不是當(dāng)前頁面。

2.元素選擇

第一個(gè)返回選中id,其余則是返回一個(gè)數(shù)組
document.getElementById()
document.getElementsByName()
document.getElementsByClassName()
document.getElementsByTagName()

Event 對象

1.獲取鼠標(biāo)的按鍵,返回按鍵索引 event.button
<script type="text/javascript">
function whichButton(event)
{
var btnNum = event.button;
if (btnNum==2){
  alert("您點(diǎn)擊了鼠標(biāo)右鍵!")
  }
else if(btnNum==0){
  alert("您點(diǎn)擊了鼠標(biāo)左鍵!")
  }
else if(btnNum==1){
  alert("您點(diǎn)擊了鼠標(biāo)中鍵!");
  }
else{
  alert("您點(diǎn)擊了" + btnNum+ "號(hào)鍵,我不能確定它的名稱。");
  }
}
</script>
</head>
<body onclick="whichButton(event)">
<p>請?jiān)谖臋n中點(diǎn)擊鼠標(biāo)。一個(gè)消息框會(huì)提示出您點(diǎn)擊了哪個(gè)鼠標(biāo)按鍵。</p>
</body>
2.獲取光標(biāo)的坐標(biāo) event.clientX
x=event.clientX
y=event.clientY  //獲取光標(biāo)的位置Y軸
3.獲取按鍵碼 event.keyCode
<script type="text/javascript">
function whichButton(event){
alert(event.keyCode)
}
</script>
</head>
<body onkeyup="whichButton(event)">
<p><b>注釋:</b>在測試這個(gè)例子時(shí),要確保右側(cè)的框架獲得了焦點(diǎn)。</p>
<p>在鍵盤上按一個(gè)鍵。消息框會(huì)提示出該按鍵的 unicode。</p>
</body>
4.獲取光標(biāo)相對于屏幕的 位置 event.screenX
x=event.screenX
y=event.screenY
6.得到事件元素 和 事件類型 vent.target event.type
function getEventType(event){ 
  console.info(event.target); //返回事件目標(biāo)元素
  console.info(event.type);  //返回事件類型 事件名稱
  }
</script>
</head>

<body onmousedown="getEventType(event)">

Form 和 Input 對象

1.復(fù)選框
<input type="checkbox" id="myCheck" />
<input type="button" onclick="check()" value="選定復(fù)選框" />
<input type="button" onclick="uncheck()" value="取消選定復(fù)選框" />
//js
document.getElementById("myCheck").checked=true //選中
document.getElementById("myCheck").checked=false //取消選中
2.獲取input的value
function check(browser)
  {
  document.getElementById("answer").value=browser
  }
</script>
</head>
<body>

<p>您喜歡哪款瀏覽器?</p>

<form>
<input type="radio" name="browser" onclick="check(this.value)" value="Internet Explorer">Internet Explorer<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Firefox">Firefox<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Netscape">Netscape<br />
<input type="radio" name="browser" onclick="check(this.value)" value="Opera">Opera<br />
<br />
您喜歡的瀏覽器是:<input type="text" id="answer" size="20">
</form>
3.form表單相關(guān)

①reset()可以清空表單數(shù)據(jù)
②submit()可以提交表單

function formReset(){
document.getElementById("myForm").reset()
}
document.getElementById("myForm").submit()
document.getElementById('text1').focus() //獲取焦點(diǎn)
document.getElementById('text1').blur() //失去焦點(diǎn)

<body>
<p>在下面的文本框中輸入一些文本,然后點(diǎn)擊重置按鈕就可以重置表單。</p>
<form id="myForm">
姓名:<input type="text" size="20"><br />
<input type="button" onclick="formReset()" value="重置">
</form>

Location 對象

window.location="/index.html" //基于原有域名下的頁面跳轉(zhuǎn)
window.location.reload(); //重載頁面
window.location , window.location.href , window,location.replace的區(qū)別

window.location是頁面的位置對象,頁面基于域名后添加新增的url跳轉(zhuǎn)
window.location.href是 location的一個(gè)屬性值,頁面替換url跳轉(zhuǎn)
如果需要打開新窗口,使用window.open('')方法

有3個(gè)頁面 a,b,c

如果當(dāng)前頁面是c頁面,并且c頁面是這樣跳轉(zhuǎn)過來的:a->b->c
1:b->c 是通過window.location.replace("..xx/c") 此時(shí)b頁面的url會(huì)被c頁面代替,并且點(diǎn)擊后退按鈕時(shí)會(huì)回退到a頁面(最開始的頁面)

2:b->c是通過window.location.href("..xx/c") 此時(shí)b頁面的路徑會(huì)被c頁面代替,但是點(diǎn)擊回按鈕后頁面回退的是b頁面

兩者的區(qū)別: 兩者后退時(shí)所回退的頁面不一樣

Navigator 對象

用于檢測和獲取瀏覽器相關(guān)信息

navigator.appName //appname
navigator.appVersion //瀏覽器的版本信息
naviigator.appCodeName //瀏覽器代碼 
navigator.cookieEnabled //是否啟用了cookie
navigator.userAgent //瀏覽器的用戶代理報(bào)頭

Option 和 Select 對象

啟用與禁用select

document.getElementById("mySelect").disabled=true
document.getElementById("mySelect").disabled=false

設(shè)置可以選取多個(gè)option

document.getElementById("mySelect").multiple=true

更改選擇項(xiàng)

document.getElementById("orange").selected=true;

window 對象

window.open() //打開新窗口
window.location //在域名后追加地址進(jìn)入新頁面
window.location.href //頁面地址
window.location.reload() //頁面重載
window.print() //打印頁面
window.scrollTo(100,500) //窗口滾動(dòng)到指定位置
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容