更新时间:2023-02-15 19:35:12
以下是一个函数,它将选择传递给它的元素的内容:
Here's a function that will select the contents of the element you pass to it:
function selectElementContents(el) {
var range;
if (window.getSelection && document.createRange) {
range = document.createRange();
var sel = window.getSelection();
range.selectNodeContents(el);
sel.removeAllRanges();
sel.addRange(range);
} else if (document.body && document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(el);
range.select();
}
}
window.onload = function() {
var el = document.getElementById("your_para_id");
selectElementContents(el);
};