日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

原生js获取document_原生JS常用API整理

發布時間:2025/3/12 javascript 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 原生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整理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。