【发布时间】:2010-09-28 14:31:18
【问题描述】:
我无法触发附加到 【问题讨论】: 元素的 onselect 事件处理程序。是否可以强制 发出 select 事件?
我无法触发附加到 【问题讨论】: 元素的 onselect 事件处理程序。是否可以强制 发出 select 事件?
根据w3schools,onSelect 只定义在 input 和 textarea 对象上。
您可以尝试在当前窗口/文档中捕捉MouseUp event 和retrieve the selected text。
【讨论】:
您可以通过使用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>
【讨论】:
使用OnClick。
:select 仅适用于某些表单元素。
【讨论】: