本篇教程介绍了HTML+CSS入门 如何输出选中文字和主动选择文字,希望阅读本篇文章以后大家有所收获,帮助大家HTML+CSS入门。
<
html>
Title12345675345
12345675345
显示选中文字
selction = window.getSelection();
const btn = document.querySelector(‘#btn‘);
const app = document.querySelector(‘.app‘);
const content = document.querySelector(‘#content‘);
const content2 = document.querySelector(‘#content2‘);
app.focus();
btn.onclick = function () {
let selction ,text;
console.log(selction.toString());//输出选中文字
alert(selction);
}
let range = selction.getRangeAt(0);
range.setStart(selction.anchorNode,selction.anchorOffset);
range.setEnd(selction.focusNode,selction.focusOffset+6);
selction.removeAllRanges();
selction.addRange(range);//主动添加选中文字
console.log(selction);
本文由职坐标整理发布,欢迎关注职坐标WEB前端HTML/CSS频道,获取更多HTML/CSS知识!