使用window.getSelection().toString()可以获取鼠标选用的内容
demo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p class="text">123</p>
<script>
window.onload = function() {
document.querySelector('.text').addEventListener('click', SelectText, true)
function SelectText() {
var selecter = window.getSelection().toString()
console.log(selecter)
}
}
</script>
</body>
</html>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…