<svelte:document> 元素允许你监听在 document 上触发的事件。这对于像 selectionchange 这种不在 window 上触发的事件特别有用。
将 onselectionchange 处理程序添加到 <svelte:document> 标签:
App
<svelte:document {onselectionchange} />避免在这个元素上使用
mouseenter和mouseleave处理程序,因为这些事件在某些浏览器的document上不会触发。请改用<svelte:body>。
上一页 下一页
1
2
3
4
5
6
7
8
9
10
11
12
13
<script>
let selection = $state(''); const onselectionchange = (e) => {selection = document.getSelection().toString();
};
</script>
<svelte:document />
<h1>Select this text to fire events</h1>
<p>Selection: {selection}</p>