将具有事件委托的事件侦听器附加到 Jupyter Notebook?
Attach an event listener with event delegation to Jupyter notebook?
我正在尝试将事件附加到 Jupyter notebook 变量(用于某些实验):
$( "#notebook-container" ).on( "dblclick", ".cm-variable", function( event ) { alert("hi"); });
由于 Jupyter notebook 变量总是 refreshed/etc,我认为将事件处理程序附加到所有变量的最佳方法是通过 jquery 事件委托:https://learn.jquery.com/events/event-delegation/
"Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future."
所以我觉得这很合适,但是,如果我将上面分享的代码行粘贴到 js 控制台中,我并没有得到预期的效果。
我想知道执行此操作的正确方法是什么,您可以在此处访问实时 jupyter 笔记本:https://try.jupyter.org/
根据 this 的回答,下面介绍了如何使用 mousedown/mouseup 模拟双击(mouseup 可能更好,因为点击是在用户抬起鼠标后触发的,而不是在他们第一次按下鼠标时触发的它)
var clicks = 0, delay = 400;
$("#notebook-container").on("mouseup", ".cm-variable", function(event) {
event.preventDefault();
clicks++;
setTimeout(function() {
clicks = 0;
}, delay);
if (clicks === 2) {
clicks = 0;
alert("hi");
return;
}
});
我正在尝试将事件附加到 Jupyter notebook 变量(用于某些实验):
$( "#notebook-container" ).on( "dblclick", ".cm-variable", function( event ) { alert("hi"); });
由于 Jupyter notebook 变量总是 refreshed/etc,我认为将事件处理程序附加到所有变量的最佳方法是通过 jquery 事件委托:https://learn.jquery.com/events/event-delegation/
"Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future."
所以我觉得这很合适,但是,如果我将上面分享的代码行粘贴到 js 控制台中,我并没有得到预期的效果。
我想知道执行此操作的正确方法是什么,您可以在此处访问实时 jupyter 笔记本:https://try.jupyter.org/
根据 this 的回答,下面介绍了如何使用 mousedown/mouseup 模拟双击(mouseup 可能更好,因为点击是在用户抬起鼠标后触发的,而不是在他们第一次按下鼠标时触发的它)
var clicks = 0, delay = 400;
$("#notebook-container").on("mouseup", ".cm-variable", function(event) {
event.preventDefault();
clicks++;
setTimeout(function() {
clicks = 0;
}, delay);
if (clicks === 2) {
clicks = 0;
alert("hi");
return;
}
});