原生JS中常用的Window和DOM對象操作匯總


 

一、常用的Window對象操作

Window對象中又包含了document、history、location、Navigator和screen幾個對象,每個對象又有自己的屬性方法,這里window可以省略。

如window.location.href  可以簡寫為location.href


//返回運行瀏覽器的操作系統和(或)硬件平台
var platform = navigator.platform;
//瀏覽器的代碼名
var appCodeName = navigator.appCodeName;
//瀏覽器的名稱
var appName = navigator.appName;
//瀏覽器的平台和版本信息
var appVersion = navigator.appVersion;
// alert("platform:"+platform+"\n appCodeName:"+appCodeName+"\n appName:"+appName+"\n appVersion:"+appVersion);

//返回歷史列表中的網址數
var length = history.length;

//加載 history 列表中的下一個 URL
history.forward();
//加載 history 列表中的前一個 URL
history.back();
//加載歷史列表中的某個具體的頁面 -1上一個頁面,1前進一個頁面
history.go(-1);

//替換當前文檔 可以是當前項目根目錄下的文檔, 或者外部 網址
location.replace("index.html");
location.replace("http://www.baidu.com");

//返回 URL的錨部分 #開始的地方
var hash = location.hash;

//返回一個URL的查詢部分 ?之后的部分
var search = location.search;

//返回完整的URL(當前頁):
var href = location.href;

//刷新當前文檔
location.reload();

//返回屏幕的總寬度和總高度
var width = window.screen.width;
var height = window.screen.height;

//在指定的毫秒數后執行
setTimeout(function(){
//TODO
},30000);

 

二、常用的HTML DOM操作

 

//通過getElementById獲取input輸入的值
var username = document.getElementById("username").value;

//通過querySelector獲取input輸入的值
var pwd = document.querySelector("#password").value;

//在id為txt的地方添加html
document.getElementById("txt").innerHTML="<h1>Hello World!</h1>";

//返回當前完整的url
var url = document.URL;

//返回當前文檔的標題
var title = document.title;

//返回當前文檔的域名
var domain = document.domain;

//向輸出流寫入文本
document.write("Hello World!");
//向輸出流寫入格式文本
document.write("<h1>Hello World!</h1>");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM