国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

html5指南-2.如何操作document metadata

2020-03-24 16:25:36
字體:
來源:轉載
供稿:網友
今天的內容是關于如何操作document對象。
1.操作Document Metadata
首先我們來看看相關的屬性:
characterSet:獲取當前document的編碼方式,該屬性為只讀;
charset:獲取或者設置當前document的編碼方式;
compatMode:獲取當前document的兼容模式
cookie:獲取或者設置當前document的cookie對象;
defaultCharset:獲取瀏覽器默認的編碼方式;
defaultView:獲取當前當前document的window對象;
dir:獲取或者設置當前document的文本對齊方式;
domain:獲取或者設置當前document的domian值;
implementation:提供所支持的dom特性的信息;
lastModified:獲取document最后的修改時間(如果沒有最后修改時間,則返回當前時間);
location:提供當前document的url信息;
readyState:返回當前document的狀態,該屬性是只讀屬性;
referrer: 返回連接到當前document的document url信息;
title:獲取或者設置當前document的title。
來看下面的例子:

復制代碼代碼如下:
!DOCTYPE html
html
head
title example /title
/head
body
script type="text/javascript"
document.writeln(' pre
document.writeln('characterSet:' + document.characterSet);
document.writeln('charset:' + document.charset);
document.writeln('compatMode:' + document.compatMode);
document.writeln('defaultCharset:' + document.defaultCharset);
document.writeln('dir:' + document.dir);
document.writeln('domain:' + document.domain);
document.writeln('lastModified:' + document.lastModified);
document.writeln('referrer:' + document.referrer);
document.writeln('title:' + document.title);
document.write(' /pre
/script
/body
/html

結果(不同瀏覽器顯示的結果可能不一樣):

2.如何理解兼容模式
compatMode屬性告訴你瀏覽器是如何處理當前document的。有太多不標準的html了,瀏覽器會試圖顯示這些頁面,即使他們不符合html規范。有些內容依賴于早先瀏覽器大戰時所存在的獨特的特性,而這些屬性石不符合規范的。compatMode會返回一個或兩個值,如下:
CSS1Compat:document符合一個有效的html規范(不一定是html5,驗證的html4頁面同樣返回這個值);
BackCompat:document包含不符合規范的特性,觸發了兼容模式。
3.使用Location對象
document.location返回一個Location對象,向你提供細粒度的document的地址信息,同時允許你導航到其他document。
protocol:獲取或者設置document url的協議;
host:獲取或者設置document url的主機信息;
href:獲取或者設置document的地址信息;
hostname:獲取或者設置document的主機名;
search:獲取或者設置document url查詢部分的信息;
hash:獲取或者設置document url hash部分的信息;
assign( url ):導航到一個指定url;
replace( url ):移除當前document,導航到指定的url;
reload():重新加載當前document;
resolveURL( url ):將相對路徑變為絕對路徑。
來看下面的例子:

復制代碼代碼如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
html xmlns="http://www.w3.org/1999/xhtml"
head
title /title
/head
body
script type="text/javascript"
document.writeln(' pre
document.writeln('protocol:' + document.location.protocol);
document.writeln('host:' + document.location.host);
document.writeln('hostname:' + document.location.hostname);
document.writeln('port:' + document.location.port);
document.writeln('pathname:' + document.location.pathname);
document.writeln('search:' + document.location.search);
document.writeln('hash:' + document.location.hash);
document.writeln(' /pre
/script
/body
/html

結果:

4.讀寫cookie
通過cookie屬性,可以對document的cookie進行新增,修改和讀取操作。如下例:

復制代碼代碼如下:
!DOCTYPE HTML
html
head
title Example /title
meta name="author" content="Adam Freeman" /
meta name="description" content="A simple example" /
/head
body
p id="cookiedata"
/p
button id="write"
Add Cookie /button
button id="update"
Update Cookie /button
button id="clear"
Clear Cookie /button
script type="text/javascript"
var cookieCount = 0;
document.getElementById('update').onclick = updateCookie;
document.getElementById('write').onclick = createCookie;
document.getElementById('clear').onclick = clearCookie;
readCookies();
function readCookies() {
document.getElementById('cookiedata').innerHTML = !document.cookie ? '' : document.cookie;
}
function updateCookie() {
document.cookie = 'cookie_' + cookieCount + '=update_' + cookieCount;
readCookies();
}
function createCookie() {
cookieCount++;
document.cookie = 'cookie_' + cookieCount + '=value_' + cookieCount;
readCookies();
}
function clearCookie() {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var arrStr = document.cookie.split("; ");
for (var i = 0; i arrStr.length; i++) {
var temp = arrStr[i].split("=");
if (temp[0]) {
document.cookie = temp[0] + "=;expires=" + exp.toGMTString();
};
}
cookieCount = 0;
readCookies();
}
/script
/body
/html

結果:

5.理解ReadyState
document.readyState幫助你了解頁面加載和解析過程中,頁面所處的當前狀態。需要記住的一點是,瀏覽器當遇到script元素時會立即執行,除非你使用defer屬性延時腳本的執行。readyState有三個值代表不同的狀態。
loading:瀏覽器正在加載和執行document;
interactive:docuent已經完成解析,但是瀏覽器正在加載其他外部資源(media,圖片等);
complete:頁面解析完成,外部資源在家完畢。
在瀏覽器整個加載和解析的過程中,readyState的值會從loading,interactive和complete逐個改變。當結合readystatechange事件(readyState狀態改變時觸發)使用,readyState會變得相當有價值。

復制代碼代碼如下:
!DOCTYPE HTML
html
head
title Example /title
meta name="author" content="Adam Freeman" /
meta name="description" content="A simple example" /
script
document.onreadystatechange = function () {
if (document.readyState == "interactive") {
document.getElementById("pressme").onclick = function () {
document.getElementById("results").innerHTML = "Button Pressed";
}
}
}
/script
/head
body
button id="pressme"
Press Me /button
pre id="results" /pre
/body
/html

上面的代碼使用readystatechange事件實現了延時執行的效果,只有當頁面上整個頁面解析接觸之后readystate的值才會變成interactive,這時再為pressme按鈕綁定click事件。這樣操作可以確保所需要的html元素都存在,防止錯誤發生。
6.獲取dom屬性實現的信息
document.implementation屬性幫助你了解瀏覽器對dom屬性的實現情況。該屬性返回DOMImplementation對象,對象包含hasFeature方法,你可以通過該方法了解瀏覽器對某屬性的實現情況。

復制代碼代碼如下:
!DOCTYPE HTML
html
head
title Example /title
meta name="author" content="Adam Freeman" /
meta name="description" content="A simple example" /
/head
body
script
var features = ["Core", "HTML", "CSS", "Selectors-API"];
var levels = ["1.0", "2.0", "3.0"];
document.writeln(" pre
for (var i = 0; i features.length; i++) {
document.writeln("Checking for feature: " + features[i]);
for (var j = 0; j levels.length; j++) {
document.write(features[i] + " Level " + levels[j] + ": ");
document.writeln(document.implementation.hasFeature(features[i], levels[j]));
}
}
document.write(" /pre ")
/script
/body
/html

效果:
html教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 调兵山市| 新竹县| 大埔县| 广安市| 枣强县| 金坛市| 砚山县| 洛阳市| 平邑县| 札达县| 象州县| 文山县| 石嘴山市| 梅州市| 册亨县| 西贡区| 会泽县| 驻马店市| 获嘉县| 朔州市| 罗甸县| 潮安县| 新巴尔虎右旗| 通山县| 志丹县| 德清县| 丹寨县| 镇巴县| 堆龙德庆县| 天全县| 昆明市| 长沙县| 屯昌县| 虹口区| 井冈山市| 丹阳市| 名山县| 壤塘县| 宁河县| 郴州市| 博白县|