Svelte基础
Advanced Svelte
Advanced reactivity
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion
正常情况下事件处理函数会在事件冒泡时被触发, 请小主注意观察,在这个示例中当你向input
输入内容时,内部的事件处理函数会被优先触发,然后是其它处理函数,因为事件的冒泡顺序是从目标对象到document。
能不能在 捕获 阶段就触发事件处理函数呢?! 当然可以,在强大的Svelte帮助下只需要在事件名称后面添加上capture
:
Normally, event handlers run during the bubbling phase. Notice what happens if you type something into the
<input>
in this example — the inner handler runs first, as the event ‘bubbles’ from the target up to the document, followed by the outer handler. Sometimes, you want handlers to run during the capture phase instead. Addcapture
to the end of the event name:
App
<div onkeydowncapture={(e) => alert(`<div> ${e.key}`)} role="presentation">
<input onkeydowncapture={(e) => alert(`<input> ${e.key}`)} />
</div>
现在事件处理函数的触发顺序就像网络热点事件一样出现反转了。另外小主还需要记得caputre
事件触发函数会优先于非capture
函数被触发哦。
Now, the relative order is reversed. If both capturing and non-capturing handlers exist for a given event, the capturing handlers will run first.
1
2
3
4
<div onkeydown={(e) => alert(`<div> ${e.key}`)} role="presentation">
<input onkeydown={(e) => alert(`<input> ${e.key}`)} />
</div>