Document节点

原文地址:https://wangdoc.com/javascript/

概述

<font color=red>document</font>节点对象代表整个文档,每张网页都有自己的<font color=red>document</font>对象。 <font color=red>window.document</font>属性就指向这个对象。只要浏览器开始载入HTML文档,该对象就存在了,可以直接使用。 <font color=red>document</font>对象有不同的办法可以获取。

  • 正常的网页,直接使用<font color=red>document</font><font color=red>window.document</font>
  • <font color=red>iframe</font>框架里面的网页,使用<font color=red>iframe</font>节点的<font color=red>contentDocument</font>属性。
  • Ajax操作返回的文档,使用<font color=red>XMLHttpRequest</font>对象的<font color=red>responseXML</font>属性。
  • 内部节点的ownerDocument属性。 <font color=red>document</font>对象继承了<font color=red>EventTarget</font>接口,<font color=red>Node</font>接口,<font color=red>ParentNode</font>接口。这意味着这些接口的方法都可以在<font color=red>document</font>对象上调用。除此之外,<font color=red>document</font>对象还有很多自己的属性和方法。

属性

快捷方式属性

以下属性是指向文档内部的某个节点的快捷方式。 (1)document.defaultView <font color=red>document.defaultView</font>属性返回<font color=red>document</font>对象所属的<font color=red>window</font>对象。如果当前文档不属于<font color=red>window</font>对象,该属性返回<font color=red>null</font>

document.defaultView === window // true

(2)document.doctype 对于HTML文档来说,<font color=red>document</font>对象一般有两个子节点。第一个子节点是<font color=red>document.doctype</font>,指向<font color=red><DOCTYPE></font>节点,即文档类型(Document Type Declaration,简写DTD)节点。HTML的文档类型节点,一般写成<font color=red><!DOCTYPE html></font>。如果没有声明DTD,该属性返回<font color=red>null</font>

var doctype = document.doctype; doctype // "<!DOCTYPE html>" doctype.name // "html"

<font color=red>document.firstChild</font>通常返回这个节点。 (3)document.documentElement <font color=red>document.documentElement</font>属性返回当前文档的根元素节点(root)。他通常是<font color=red>document</font>节点的第二个子节点,紧跟在<font color=red>document.doctype</font>节点后面。HTML网页的该属性,一般是<font color=red><html></font>节点。 (4)document.body,document.head <font color=red>document.body</font>属性指向<font color=red><body></font>节点,<font color=red>document.head</font>属性指向<font color=red><head></font>节点。 这两个属性总是存在的,如果网页源码省略了<font color=red><head></font><font color=red><body></font>,浏览器会自动创建。另外,这两个属性是可写的,如果改写它们的值,相当于移除所有子节点。 (5)document.scrollingElement <font color=red>document.scrollingElement</font>属性返回文档的滚动元素。也就是说,当文档整体滚动时,到底是哪个元素在滚动。 标准模式下,该属性返回文档的根元素<font color=red>document.documentElement</font>(即<font color=red><html></font>)。兼容模式下,返回的是<font color=red><body></font>元素不存在,返回<font color=red>null</font>

document.scrollingElement.scrollingTop = 0; // 页面滚动到浏览器顶部

(6)document.activeElement <font color=red>document.activeElement</font>属性返回获得当前焦点(focus)的DOM元素。通常,这个属性返回的是<font color=red><input></font><font color=red><textarea></font><font color=red><select></font>等表单元素,如果当前没有焦点元素,返回<font color=red><body></font>元素或<font color=red>null</font>(7)document.fullscreenElement <font color=red>document.fullscreenElement</font>属性返回当前以全屏状态展示的DOM元素。如果不是全屏状态,该属性返回<font color=red>null</font>

if (document.fullscreenElement.nodeName = "VIDEO") { console.log("全屏播放视频"); }

上面代码中,通过<font color=red>document.fullscreenElement</font>可以知道<font color=red><video></font>元素有没有处在全屏状态,从而判断用户行为。

节点集合属性

以下属性返回一个<font color=red>HTMLCollection</font>实例,表示文档内部特定元素的集合。 (1)document.links <font color=red>document.links</font>属性返回当前文档所有设定了<font color=red>href</font>属性的<font color=red><a></font><font color=red><area></font>节点。 (2)document.forms <font color=red>document.forms</font>属性返回所有<font color=red><form></font>表单节点。 <font color=red>id</font>属性和<font color=red>name</font>属性也可以用来引用表单。

// HTML 代码 // <form name="foo" id="bar"></form> document.forms[0] === document.forms.foo // true document.forms.bar === document.forms.foo // true

(3)document.images <font color=red>document.images</font>属性返回页面所有<font color=red><img></font>图片节点。 (4)document.embeds,document.plugins <font color=red>document.embeds</font>属性和<font color=red>document.plugins</font>属性,都返回所有<font color=red><embed></font>节点。 (5)document.scripts <font color=red>document.scripts</font>属性返回所有<font color=red><script></font>节点。 (6)document.styleSheets <font color=red>document.styleSheets</font>属性返回文档内嵌或引入的样式表集合。 (7)小结 除了<font color=red>document.styleSheets</font>,以上的集合属性返回的都是<font color=red>HTMLCollection</font>实例。

文档静态信息属性

以下属性返回文档信息 (1)document.documentURI,document.URL <font color=red>document.documentURI</font>属性和<font color=red>document.URL</font>属性都返回一个字符串,表示当前文档的网址。不同之处在于它们继承自不同的接口,<font color=red>documentURI</font>继承自<font color=red>Document</font>接口,可用于所有文档;<font color=red>URL</font>继承自<font color=red>HTMLDocument</font>接口,只能用于HTML文档。如果文档的锚点(<font color=red>#anchor</font>)变化,这两个属性都会跟着变化。 (2)document.domain <font color=red>document.domain</font>属性返回当前文档的域名,不包含协议和接口。比如,网页的网址是<font color=red>http://www.example.com:80/hello.html</font>,那么<font color=red>domain</font>属性就等于<font color=red>www.example.com</font>。如果无法获取域名,该属性返回<font color=red>null</font><font color=red>document.domain</font>基本上是一个只读属性,只有一种情况除外。次级域名的网页,可以把<font color=red>document.domain</font>设为对应的上级域名。比如,当前域名是<font color=red>a.sub.example.com</font>,则<font color=red>document.domain</font>属性可以设置为<font color=red>sub.example.com</font>,也可以设为<font color=red>example.com</font>。修改后,<font color=red>document.domain</font>相同的两个网页,可以读取对方的资源,比如设置的Cookie。 另外,设置<font color=red>document.domain</font>会导致端口被改成<font color=red>null</font>。因此,如果通过设置<font color=red>document.domain</font>来进行通信,双方网页都必须设置这个值,才能保证端口相同。 (3)document.location <font color=red>Location</font>对象是浏览器提供的原生对象,提供URL相关的信息和操作方法。通过<font color=red>window.location</font><font color=red>document.location</font>属性,可以拿到这个对象。 (4)document.lastModified <font color=red>document.lastModified</font>属性返回一个字符串,表示当前文档最后修改的时间。不同浏览器的返回值,日期格式是不一样的。 注意,<font color=red>document.lastModified</font>属性的值是字符串,所以不能直接用来比较。<font color=red>Date.parse</font>方法将其转为<font color=red>Date</font>实例,才能比较。如果页面上有JavaScript生成的内容,<font color=red>document.lastModified</font>属性返回的总是当前时间(5)document.title <font color=red>document.title</font>属性返回当前文档的标题。默认情况下,返回<font color=red><title></font>节点的值。但是该属性是可写的,一旦被修改,就返回修改的值。 (6)document.characterSet <font color=red>document.characterSet</font>属性返回当前文档的编码,比如<font color=red>UTF-8</font><font color=red>ISO-8859-1</font>等等。 (7)document.referrer <font color=red>document.referrer</font>属性返回一个字符串,表示当前文档的访问者来自哪里。如果无法获取来源,或者用户直接键入网址而不是从其他网页点击进入,<font color=red>document.referrer</font>返回一个空字符串。 (8)document.dir <font color=red>document.dir</font>返回一个字符串,表示文字方向。他只有两个可能的值:<font color=red>rtl</font>表示文字从右到左,阿拉伯文是这种方式;<font color=red>ltr</font>表示从左到右。 (9)document.compatMode <font color=red>compatMode</font>属性返回浏览器文档处理模式,可能值为<font color=red>BackCompat</font>向后兼容模式和<font color=red>CSS1Compat</font>严格模式。 一般来说,如果网页代码第一行设置了明确的<font color=red>DOCTYPE</font><font color=red>document.compatMode</font>的值都为<font color=red>CSS1Compat</font>

文档状态属性

(1)document.hidden <font color=red>document.hidden</font>属性返回一个布尔值,表示当前页面是否可见。如果窗口最小化、浏览器切换了Tab,都会导致页面不可见,使得<font color=red>document.hidden</font>返回<font color=red>true</font>(2)document.visibilityState <font color=red>document.visibilityState</font>返回文档的可见状态。 它的值有四种可能。

  • <font color=red>visible</font>:页面可见。注意,页面可能是部分可见,即不是焦点窗口,前面被其他窗口部分挡住了。

  • <font color=red>hidden</font>:页面不可见,有可能窗口最小化,或者浏览器切换到另一个Tab。

  • <font color=red>prerender</font>:页面处于正在渲染状态,对于用户来说,该页面不可见。

  • <font color=red>unloaded</font>:页面从内存里面卸载了。 这个属性可以用来页面加载时,防止加载某些资源;或者页面不可见时,停掉一些页面功能。 (3)document.readyState <font color=red>document.readyState</font>属性返回当前文档的状态,总共有三种可能的值。

  • <font color=red>loading</font>:加载HTML代码阶段(尚未完成解析)

  • <font color=red>interactive</font>:加载外部资源阶段

  • <font color=red>complete</font>:加载完成 这个属性变化过程如下: 1、浏览器开始解析HTML文档,<font color=red>document.readyState</font>属性等于<font color=red>loading</font>。 2、浏览器遇到HTML文档中的<font color=red><script></font>元素,并且没有<font color=red>async</font><font color=red>defer</font>属性,就暂停解析,开始执行脚本,这时<font color=red>document.readyState</font>属性还是等于<font color=red>loading</font>。 3、HTML文档解析完成,<font color=red>document.readyState</font>属性变成<font color=red>interactive</font>。 4、浏览器等待图片、样式表、字体文件等外部资源加载完成,一旦全部全部加载完成,<font color=red>document.readyState</font>属性变成<font color=red>complete</font>

    var interval = setInterval(function () { if (document.readyState === "complete") { clearInterval(interval); // ... } }, 100);

另外每次状态变化都会触发一个<font color=red>readystatechange</font>事件。

document.cookie

<font color=red>document.cookie</font>属性用来操作浏览器Cookie。

document.designMode

<font color=red>document.designMode</font>属性控制当前文档是否可编辑。该属性只有两个值<font color=red>on</font><font color=red>off</font>,默认值是<font color=red>off</font>。一旦设为<font color=red>on</font>。用户就可以编辑整个文档的内容。 下面代码打开<font color=red>iframe</font>元素内部文档的<font color=red>designMode</font>属性,就能将其变成一个所见即所得的编辑器。

// HTML 代码 // <iframe id="editor" src="about:blank"></iframe> var editor = document.getElementById("editor"); editor.contentDocument.designMode = "on";

document.implementation

<font color=red>document.implementation</font>属性返回一个<font color=red>DOMImplementation</font>对象。该对象有三个方法,主要用于创建独立于当前文档的新的Document对象。

  • <font color=red>DOMImplementation.createDocument()</font>:创建一个XML文档

  • <font color=red>DOMImplementation.createHTMLDocument()</font>:创建一个HTML文档

  • <font color=red>DOMImplementation.createDocumentType()</font>:创建一个DocumentType对象 下面是创建HTML文档的例子。

    var doc = document.implementation.createHTMLDocument("Title"); var p = doc.createElement("p"); p.innerHTML = "hello world"; doc.body.appendChild(p);

    document.replaceChild(doc.documentElement, document.documentElement);

方法

document.open(),document.close()

<font color=red>document.open</font>方法清除当前文档所有内容,使得文档处于可写状态,供<font color=red>document.write</font>方法写入内容。<font color=red>document.close</font>方法用来关闭<font color=red>document.open</font>打开的文档。

document.write(),document.writeln()

<font color=red>document.write</font>方法用于向当前文档写入内容。 在网页的首次渲染阶段,只要页面没有关闭写入(即没有执行document.close()),<font color=red>document.write</font>写入的内容就会追加在已有内容的后面。 注意,<font color=red>document.write</font>会将<font color=red><p></font>当作HTML标签解释。 如果页面已经解析完成(<font color=red>DOMContentLoaded</font>事件发生之后),再调用<font color=red>write</font>方法,它会先调用<font color=red>open</font>方法,擦除当前文档所有内容,然后再写入。 <font color=red>document.write</font>是JavaScript语音标准化之前就存在的方法,现在完全有更符合标准的方法问的那个写入内容(比如对innerHTML属性赋值)。所以,除了某些特殊的情况,应该尽量避免使用<font color=red>document.write</font>这个方法。 <font color=red>document.writeln</font>方法与<font color=red>write</font>方法完全一致,除了会在输出内容的尾部添加换行符。 注意,<font color=red>writeln</font>方法添加的ASCII码的换行符,渲染成HTML页面时不起作用。

document.querySelector(),document.querySelectorAll()

<font color=red>document.querySelector</font>方法接受一个CSS选择器作为参数,返回匹配该选择器的元素节点。如果有多个节点匹配条件,则返回第一个匹配的节点。没有就返回<font color=red>null</font>. <font color=red>document.querySelectorAll</font>返回一个<font color=red>NodeList</font>对象,包含所有匹配选择器节点。 这两个方法的参数,可以是逗号分隔的多个CSS选择器,返回匹配其中一个选择器的元素节点,这与CSS选择器的规则是一致的。

var matches = document.querySelectorAll("div.note", "div.alert");

这两个方法都支持复杂的CSS选择器。 但是,不支持CSS伪元素的选择器(比如<font color=red>:first-line</font><font color=red>:first-letter</font>)和伪类的选择器(比如<font color=red>:link</font><font color=red>:visited</font>),即无法选中伪元素和伪类。 如果<font color=red>querySelectorAll</font>方法的参数是字符串<font color=red>*</font>,则会返回文档中的所有元素节点。另外,<font color=red>querySelectorAll</font>的返回结果不是动态集合,不会实时反映元素节点的变化。 最后,这两个方法除了定义在<font color=red>document</font>对象上,还定义在元素节点上,即在元素节点上也可以调用。

document.getElementsByTagName()

<font color=red>document.getElementsByTagName</font>方法搜索HTML标签名,返回值是一个类似数组对象(<font color=red>HTMLCollection</font>实例)。 HTML标签名是大小写不敏感的,因此<font color=red>getElementsByTagName</font>方法也是大小写不敏感的。另外结果中,各个成员的顺序就是它们在文档中出现的顺序。 如果传入<font color=red>*</font>,就可以返回文档中所有HTML元素。 注意,元素节点本身也定义了<font color=red>getElementsByTagName</font>方法,返回该元素的后代元素中符合条件的元素。

document.getElementsByClassName()

<font color=red>document.getElementsByClassName</font>方法返回一个类似数组的对象(<font color=red>HTMLCollection</font>实例),包括了所有<font color=red>class</font>名字符合指定条件的元素,元素的变化实时反映在返回结果中。

var elements = document.getElementsByClassName(names);

由于<font color=red>class</font>是保留字,所以JavaScript一律使用<font color=red>className</font>表示CSS的<font color=red>class</font>。参数可以是多个<font color=red>class</font>,它们之间使用空格分隔。

var elements = document.getElementsByClassName("foo bar");

上面代码返回同时具有<font color=red>foo</font><font color=red>bar</font>两个<font color=red>class</font>的元素,<font color=red>foo</font><font color=red>bar</font>顺序不重要。 注意,正常模式下,CSS的<font color=red>class</font>是大小写敏感的。(<font color=red>quirks mode</font>下,大小写不敏感) <font color=red>getElementsByClassName</font>方法不仅可以在<font color=red>document</font>对象上调用,也可以在任何元素节点上调用。

document.getElementsByName()

<font color=red>document.getElementsByName</font>方法用于选择拥有<font color=red>name</font>属性的HTML元素(比如<font color=red><form></font><font color=red><radio></font><font color=red><img></font><font color=red><frame></font><font color=red><embed></font><font color=red><object></font>等),返回一个类似数组的对象(NodeList实例)。

document.getElementById()

<font color=red>document.getElementById</font>方法返回匹配指定<font color=red>id</font>属性的元素节点。如果没有发现匹配的节点,则返回<font color=red>null</font>。 注意,<font color=red>document.getElementById</font>方法的参数是大小写敏感的。 <font color=red>document.getElementById</font>方法与<font color=red>document.querySelector</font>方法都能获取元素节点,不同之处是<font color=red>document.querySelector</font>方法的参数使用CSS选择器语法,<font color=red>document.getElementById</font>方法参数是元素的<font color=red>id</font>属性。

document.getElementById("myElement"); document.querySelector("#myElement");

document.elementFromPoint(),document.elementsFromPoint()

<font color=red>document.elementFromPoint</font>方法返回位于页面指定位置最上层的元素节点。

var element = document.elementFromPoint(50, 50);

上面代码选中在<font color=red>(50, 50)</font>这个坐标位置的最上面的那个HTML元素。 <font color=red>elementFromPoint</font>方法的两个参数分别是点相对于左上角的横纵坐标,单位是像素。如果位于该位置的HTML元素不可返回(比如文本框的滚动条),则返回它的父元素(比如文本框)。如果左边无意义,返回<font color=red>null</font>

document.caretPositionFromPoint()

<font color=red>document.carePositionFromPoint</font>方法返回一个<font color=red>CaretPosition</font>对象,包含了指定坐标点在节点对象内部的位置信息。<font color=red>CaretPosition</font>对象就是光标插入点的概念,用于确定光标点在文本对象内部的具体位置。

var range = document.caretPositionFromPoint(clientX, clientY);

上面代码中,<font color=red>range</font>是指定坐标点的<font color=red>CaretPosition</font>对象。该对象有两个属性。

  • CaretPosition.offsetNode:该位置的节点对象
  • CaretPosition.offset:该位置在<font color=red>offsetNode</font>对象内部,与初始位置相距的字符数。

document.createElement()

<font color=red>document.createElement</font>方法用来生成元素节点,并返回该节点。<font color=red>createElement</font>方法的参数为元素标签名,即元素节点的<font color=red>tagName</font>属性。如果参数里面包含尖括号会报错。

document.createTextNode()

<font color=red>document.createTextNode</font>方法用来生成文本节点(<font color=red>Text</font>实例),并返回该节点。它的参数是文本节点的内容。 这个方法可以确保返回的节点,被浏览器当作文本渲染,而不是当作HTML代码渲染。因此,用来展示用户的输入,避免XSS攻击需要注意的是,该方法不对单引号和双引号转义,所以不能用来对HTML属性赋值

document.createAttribute()

<font color=red>document.createAttribute</font>方法生成一个新的属性节点(<font color=red>Attr</font>实例),并返回它。 例如:

var node = document.getElementById("div1"); var a = document.createAttribute("my_attrib"); a.value = "new_val"; node.setAttributeNode(a); // 或者 node.setAttribute("my_attrib", "new_val");

document.createComment()

<font color=red>document.createComment</font>方法生成一个新的注释节点,并返回该节点。

document.createDocumentFragment()

<font color=red>document.createDocumentFragment</font>方法生成一个空的文档片段对象(<font color=red>DocumentFragment</font>实例)。 <font color=red>DocumentFragment</font>是一个存在于内存的DOM片段,不属于当前文档,常常用来生成一段较复杂的DOM结构,然后再插入当前文档。这样做的好处在于,因为<font color=red>DocumentFragment</font>不属于当前文档,对它的任何改动,都不会引发网页的重新渲染,比直接修改当前文档的DOM有更好的性能表现。

var docFrag = document.createDocumentFragment(); [1, 2, 3, 4].forEach(function (e) { var li = document.createElement("li"); li.textContent = e; docfrag.appendChild(li); }); var element = document.getElementById("ul"); element.appendChild(docfrag);

document.createEvent()

<font color=red>document.createEvent</font>方法生成一个事件对象(<font color=red>Event</font>实例),该对象可以被<font color=red>element.dispatchEvent</font>方法使用,触发指定事件。 <font color=red>document.createEvent</font>方法参数是事件类型,比如<font color=red>UIEvents</font><font color=red>MouseEvents</font><font color=red>MutationEvents</font><font color=red>HTMLEvents</font>

var event = document.createEvent("Event"); event.initEvent("build", true, true); document.addEventListener("build", function (e) { console.log(e.type); // "build" }, false); document.dispatchEvent(event);

document.addEventListener(),document.removeEventListener(),document.dispatchEvent()

这三个方法用于处理<font color=red>document</font>节点的事件。它们都继承自<font color=red>EventTarget</font>接口,详细介绍参见《EventTarget接口》。

document.addEventListener("click", listener, false); document.removeEventListener("click", listener, false); var event = new Event("click"); document.dispatchEvent(event);

document.hasFocus()

<font color=red>document.hasFocus</font>方法返回一个布尔值,表示当前文档之中是否有元素被激活或获得焦点。

var focused = document.hasFocus();

注意,有焦点的文档必定被激活(active),反之不成立,激活的文档未必有焦点。比如用户点击按钮,从当前窗口跳出新窗口,该新窗口是激活的,但是不拥有焦点。

document.adoptNode(),document.importNode()

<font color=red>document.adoptNode</font>方法将某个节点及其子节点,从原来所在的文档或<font color=red>DocumentFragment</font>里面移除,归属当前<font color=red>document</font>对象,返回插入后的新节点。插入的节点对象的<font color=red>ownerDocument</font>属性会变成当前的<font color=red>document</font>对象,而<font color=red>parentNode</font>属性是<font color=red>null</font>

var node = document.adoptNode(externalNode); document.appendChild(node);

注意,<font color=red>document.adoptNode</font>方法只是改变了节点的归属,并没有将这个节点插入新的文档树。所以,还要再用<font color=red>appendChild</font>方法或者<font color=red>insertBefore</font>方法,将新节点插入当前文档树。 <font color=red>document.importNode</font>方法则是从原来所在的文档或<font color=red>DocumentFragment</font>里面,拷贝某个节点及其子节点,让它们归属当前<font color=red>document</font>对象。拷贝的节点对象的<font color=red>ownerDocument</font>属性,会变成当前的<font color=red>document</font>对象,而<font color=red>parentNode</font>属性是<font color=red>null</font>

var node = document.importNode(externalNode, deep);

<font color=red>document.importNode</font>方法的第一个参数是外部节点,第二个参数是一个布尔值,表示对外部节点是深拷贝还是浅拷贝(false)。虽然第二个参数是可选的,但是建议总是保留这个参数,并设为<font color=red>true</font>。 注意,<font color=red>document.importNode</font>方法只是拷贝外部节点,这时该节点的父节点是<font color=red>null</font>。下一步还必须将这个节点插入当前文档树。

var iframe = document.getElementsByTagName("iframe")[0]; var oldNode = iframe.contentWindow.document.getElementById("myNode"); var newNode = document.importNode(oldNode, true); document.getElementById("container").appendChild(newNode);

上面代码从<font color=red>iframe</font>窗口拷贝一个指定节点<font color=red>myNode</font>,插入当前文档。

document.createNodeIterator()

<font color=red>document.createNodeIterator</font>方法返回一个子节点遍历器。

var nodeIterator = document.createNodeIterator(document.body, NodeFilter.SHOW_ELEMENT);

上面代码返回<font color=red><body></font>元素子节点的遍历器。 <font color=red>document.createNodeIterator</font>方法第一个参数为所要遍历的根节点,第二个参数是所要遍历的节点类型,这里指定为元素节点(<font color=red>NodeFilter.SHOW_ELEMENT</font>)。几种主要的节点类型写法如下。

  • 所有节点:NodeFilter.SHOW_ALL

  • 元素节点:NodeFilter.SHOW_ELEMENT

  • 文本节点:NodeFilter.SHOW_TEXT

  • 评论节点:NodeFilter.SHOW_COMMENT <font color=red>document.createNodeIterator</font>方法返回一个遍历器对象(<font color=red>NodeFilter</font>实例)。该实例的<font color=red>nextNode</font>方法和<font color=red>previousNode</font>方法,可以用来遍历所有子节点。

    var nodeIterator = document.createNodeIterator(document.body); var pars = []; var currentNode;

    while (currentNode = nodeIterator.nextNode()) { pars.push(currentNode); }

上面代码中,使用遍历器的<font color=red>nextNode</font>方法,将根节点的所有子节点依次读入一个数组。<font color=red>nextNode</font>方法先返回遍历器的内部指针所在的节点,然后将指针移向下一个节点。所有成员遍历完成后,返回<font color=red>null</font><font color=red>previousNode</font>方法则是先将指针移向上一个节点,然后返回该节点。 注意,遍历器返回的第一个节点,总是根节点

document.createTreeWalker()

<font color=red>document.createTreeWalker</font>方法返回一个DOM的子树遍历器。它与<font color=red>document.createNodeIterator</font>方法基本类似,区别在于它返回的是<font color=red>TreeWalker</font>实例,后者返回的是<font color=red>NodeIterator</font>实例。另外,它的第一个节点不是根节点。 <font color=red>document.createTreeWalker</font>方法的第一个参数是所要遍历的根节点,第二个参数指定所要遍历的节点类型(与<font color=red>document.createNodeIterator</font>方法的第二个参数相同)。

var treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT); var nodeList = []; while (treeWalker.nextNode()) { nodeList.push(treeWalker.currentNode); }

document.execCommand(),document.queryCommandSupported(),document.queryCommandEnabled()

如果<font color=red>document.designMode</font>属性设为<font color=red>on</font>,那么整个文档用户可编辑;如果元素的<font color=red>contenteditable</font>属性设为<font color=red>true</font>,那么该元素可编辑。这两种情况下,可以使用<font color=red>document.execCommand</font>方法,改变内容的样式,比如<font color=red>document.execCommand("bold")</font>会使字体加粗。

document.execCommand(command, showDefaultUI, input)

该方法接受三个参数。

  • <font color=red>command</font>:字符串,表示所要实施的样式。

  • <font color=red>showDefaultUI</font>:布尔值,表示是否要使用默认的用户界面,建议总是设为<font color=red>false</font>

  • <font color=red>input</font>:字符串,表示该样式的辅助内容,比如生成超级链接时,这个参数就是所要链接的网址。如果第二个参数设为<font color=red>true</font>,那么浏览器会弹出提示框,要求用户在提示框输入改参数。但是,不是所有浏览器都支持这样做,为了兼容性,还是需要自己部署这个参数的获取方式。

    var url = window.prompt("请输入网址");

    if (url) { document.execCommand("createlink", false, url); }

上面代码中,先提示用户输入所要链接的网址,然后手动生成超级链接。注意,第二个参数是<font color=red>false</font>,表示此时不需要自动弹出提示框。 <font color=red>document.execCommand()</font>的返回值是一个布尔值。如果为<font color=red>false</font>,表示这个方法无法生效。 这个方法大部分情况下,只对选中内容生效。如果有多个内容可编辑区域,那么只对当前焦点所在的元素生效。 <font color=red>document.execCommand()</font>方法可以执行的样式改变有很多种,下面是其中的一些:bold、insertLineBreak、selectAll、createLink、insertOrderedList、subscript、delete、insertUnorderedList、superscript、formatBlock、insertParagraph、undo、forwardDelete、insertText、unlink、insertImage、italic、unselect、insertHTML、redo。这些值都可以用作第一个参数,它的含义不难从字面上看出来。 <font color=red>document.queryCommandEnabled()</font>方法返回一个布尔值,表示浏览器是否允许使用这个方法。

if (document.queryCommandEnabled("selectAll")) { // ... }

<font color=red>document.queryCommandSupported</font>方法返回一个布尔值,表示当前是否可用某种样式改变。比如,加粗只有存在文本选中时才能使用,如果没有选中文本,就不可用。

if (document.queryCommandSupported("selectAll")) { // ... }

<font color=red>document.queryCommandSupported()</font>方法返回一个布尔值。

document.getSelection()

这个方法指向<font color=red>window.getSelection()</font>,参见<font color=red>window</font>对象一节。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

swap空间的增减方法

(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap