<svelte:document> 元素允许你监听在 document 上触发的事件。这在处理像 selectionchange 这样的事件时很有用,因为这些事件不会在 window 上触发。
将 onselectionchange 处理程序添加到 <svelte:document> 标签
应用
<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>