If you have 10 buttons inside a p, attach only one event handler to
the p wrapper, instead of one handler for each button. Events bubble
up so you'll be able to catch the event and figure out which button it
originated from.如果在一个p中有10个按钮,与其在每个按钮上都放一个事件处理程序,不如只在p上放一个事件处理程序。事件会冒泡上溯,这样你就会捕获这一事件,并找出是哪个按钮发起的它。
我设定了如下场景如果使用jquery来写下,例如:
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
Event Delegation, 事件代理, Backbone 里经常这么干的, 原理前面的答案说了.
区分的步骤, 不是用
switch/case, 而是用 jQuery 选择器, jQuery 文档里找找细节.