您可以使用onMouseUp
事件来实现您想要的效果.见下文...
<html>
<body>
<div id="container" style="border: 1px solid #333" contentEditable="true" onMouseUp="checkMe()">Type text here</div>
</body>
<script language="javascript">
function checkMe() {
var txt = "";
if (window.getSelection) {
txt = window.getSelection();
}
else if (document.getSelection) {
txt = document.getSelection();
} else if (document.selection) {
txt = document.selection.createRange().text;
}
alert("Selected text is " + txt);
}
</script>
</html>
Run Code Online (Sandbox Code Playgroud)