javascript
原生js获取document_原生JS常用API整理
Anchor 對象:指HTML超鏈接
1.修改一個鏈接的文本,鏈接和target
//html部分
訪問 Microsoft
改變超鏈接的文本和 URL。也改變 target 屬性。target 屬性的默認設置是 "_self",這意味著會在相同的窗口中打開鏈接。通過把 target 屬性設置為 "_blank",鏈接將在新窗口中打開。
//js部分
function changeLink(){
document.getElementById(‘myAnchor’).innerHTML=“訪問 W3School"
document.getElementById(‘myAnchor’).href=“http://www.w3school.com.cn"
document.getElementById(‘myAnchor’).target=”_blank”
}
Document 對象:指 頁面文檔文本
1.document輸出流輸出 文本 或者 HTML
document.write(“Hello World!”) //Hello World
document.write(“
Hello World!”) //帶h1標簽效果的 Hello World
document.write(document.title) //返回當前頁面的標題
document.write(document.URL) //返回當前頁面的URL
document.URL , document.referrer , location.href 的區別:
①. 從輸出結果上,document.URL 和 windows.location.href 沒有區別。
②. 非要說區別的話,你只可以讀取document.URL的值,不能修改它。windows.location.href的值你即可以讀取也可以修改,可以使用它進行頁面跳轉。
③. referrer則是返回前一個來源頁面的URL,并不是當前頁面。
2.元素選擇
第一個返回選中id,其余則是返回一個數組
document.getElementById()
document.getElementsByName()
document.getElementsByClassName()
document.getElementsByTagName()
Event 對象
1.獲取鼠標的按鍵,返回按鍵索引 event.button
請在文檔中點擊鼠標。一個消息框會提示出您點擊了哪個鼠標按鍵。
2.獲取光標的坐標 event.clientX
x=event.clientX
y=event.clientY //獲取光標的位置Y軸
3.獲取按鍵碼 event.keyCode
Form 和 Input 對象
1.復選框
//js
document.getElementById("myCheck").checked=true //選中
document.getElementById("myCheck").checked=false //取消選中
2.獲取input的value
function check(browser)
{
document.getElementById("answer").value=browser
}
您喜歡哪款瀏覽器?
Internet Explorer
Firefox
Netscape
Opera
您喜歡的瀏覽器是:
3.form表單相關
①reset()可以清空表單數據
②submit()可以提交表單
function formReset(){
document.getElementById(“myForm”).reset()
}
document.getElementById(“myForm”).submit()
document.getElementById(‘text1’).focus() //獲取焦點
document.getElementById(‘text1’).blur() //失去焦點
在下面的文本框中輸入一些文本,然后點擊重置按鈕就可以重置表單。
姓名:
Location 對象
window.location="/index.html" //基于原有域名下的頁面跳轉
window.location.reload(); //重載頁面
window.location , window.location.href , window,location.replace的區別
window.location是頁面的位置對象,頁面基于域名后添加新增的url跳轉
window.location.href是 location的一個屬性值,頁面替換url跳轉
如果需要打開新窗口,使用window.open('')方法
有3個頁面 a,b,c
如果當前頁面是c頁面,并且c頁面是這樣跳轉過來的:a->b->c
1:b->c 是通過window.location.replace("…xx/c") 此時b頁面的url會被c頁面代替,并且點擊后退按鈕時會回退到a頁面(最開始的頁面)
2:b->c是通過window.location.href("…xx/c") 此時b頁面的路徑會被c頁面代替,但是點擊回按鈕后頁面回退的是b頁面
兩者的區別: 兩者后退時所回退的頁面不一樣
Navigator 對象
用于檢測和獲取瀏覽器相關信息
navigator.appName //appname
navigator.appVersion //瀏覽器的版本信息
naviigator.appCodeName //瀏覽器代碼
navigator.cookieEnabled //是否啟用了cookie
navigator.userAgent //瀏覽器的用戶代理報頭
Option 和 Select 對象
啟用與禁用select
document.getElementById(“mySelect”).disabled=true
document.getElementById(“mySelect”).disabled=false
設置可以選取多個option
document.getElementById(“mySelect”).multiple=true
更改選擇項
document.getElementById(“orange”).selected=true;
window 對象
window.open() //打開新窗口
window.location //在域名后追加地址進入新頁面
window.location.href //頁面地址
window.location.reload() //頁面重載
window.print() //打印頁面
window.scrollTo(100,500) //窗口滾動到指定位置
總結
以上是生活随笔為你收集整理的原生js获取document_原生JS常用API整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java httppost wsdl_J
- 下一篇: 考研规划计算机科学与技术,2021考研: