简体   繁体   中英

createRange select all in parent js

I'm looking to select a range with javascript createRange function. I'm just now after so many years discovering this function, and it's one of those DOH! moments.

Is there a way to select all the elements in a given parent. Currently, I have a div full of content I'm trying to select. This content may be text nodes, elements, IDK.

var container = document.getElementById('mydiv');
var range = document.createRange();
range.setStart(container, 0);
range.setEnd(container, ???);

Is there an accurate, easy, efficient way to get the length of content in the parent ( container )? Or is there a hidden method with range that does this already?

No libraries, please! Not terribly concerned with cross-browser fall-backs, but insights into them are welcome if you have any.

EDIT

For clarity, I'm NOT looking to get the innerHTML, but rather a range object. So PLEASE don't say "why not just use innerHTML".

selectNodeContents可以解决问题,获取指定元素的内容。

range.selectNodeContents(container);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM