Range.collapse()

您正在阅读此内容的英文版本,因为该语系尚未翻译。 帮助我们翻译此文章吧!

Range.collapse() 方法向边界点折叠该 Range

折叠后的 Range 为空,不包含任何内容。

要确定 Range 是否已折叠,使用Range.collapsed 属性。

语法

range.collapse(toStart);

参数

toStart 可选
一个布尔值: true 折叠到 Range 的 start 节点,false 折叠到 end 节点。如果省略,则默认为 false .

例子

var range = document.createRange();

referenceNode = document.getElementsByTagName("div").item(0);
range.selectNode(referenceNode);
range.collapse(true);

规范

Specification Status Comment
DOM
Range.collapse()
Living Standard The parameter is now optional and default to false.
Document Object Model (DOM) Level 2 Traversal and Range Specification
Range.collapse()
Obsolete Initial specification.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
collapseChrome Full support YesEdge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
toStart parameter optional
Experimental
Chrome Full support YesEdge ? Firefox Full support 25IE No support No
Notes
No support No
Notes
Notes Optional but defaults to true.
Opera Full support 15Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

参阅