我有以下非常简单的 html 页面:
<html>
<head>
<script type="text/javascript">
function alertSelection()
{
var selection = window.getSelection();
var txt = selection.toString();
alert(txt);
}
</script>
</head>
<body>
This is <span style="background-color:black;color:white">the</span> text.
<div style="background-color:green;width:30px;height:30px;margin:30px"
onmouseover="alertSelection()">
</body>
</html>
当我选择整个第一行并将鼠标悬停在正方形上时,我会收到“这是文本。”的警报。
我将如何解决此问题,以便不会从警报消息中删除 span 标签或任何其他选定的 HTML?
编辑:我正在专门寻找如何从window.getSelection()
. 警报对话框正是我尝试验证代码的方式。我只关心在 Safari 中的工作。