--- title: Selection.addRange() slug: Web/API/Selection/addRange translation_of: Web/API/Selection/addRange ---
向选区({{domxref("Selection")}})中添加一个区域({{domxref("Range")}})。
sel.addRange(range)
range
/* 在一个HTML文档中选中所有加粗的文本。 */ var strongs = document.getElementsByTagName("strong"); var s = window.getSelection(); if(s.rangeCount > 0) s.removeAllRanges(); for(var i = 0; i < strongs.length; i++) { var range = document.createRange(); range.selectNode(strongs[i]); s.addRange(range); }
规范 | 状态 | 注释 |
---|---|---|
{{SpecName('HTML Editing', '#dom-selection-addrange', 'Selection.addRange()')}} | {{Spec2('HTML Editing')}} | Initial definition |
{{SpecName('Selection API', '#widl-Selection-addRange-void-Range-range', 'Selection.addRange()')}} | {{Spec2('Selection API')}} | Current |
{{Compat("api.Selection.addRange")}}