Ale*_*yer 12 javascript onclick dom-events onclicklistener
我试图在附加到按钮的另一个单击事件中向文档添加单击事件.但是,第二次单击事件会立即触发,就像事件重叠一样.我考虑停止传播,使用超时,删除监听器,preventDefault(),但我没有成功.
这是我想要做的一个例子.
document.getElementById("test").addEventListener('click', first);
function first(){
document.addEventListener('click', second);
}
function second(){
alert("I'm not suppose to appear after the first click, only the second.");
}
Run Code Online (Sandbox Code Playgroud)
为了测试,我使用一个简单的按钮
<button type="button" id="test">Click</button>
Run Code Online (Sandbox Code Playgroud)
我没有JQuery这样做.这可能吗?
gue*_*314 13
尝试使用 event.stopImmediatePropagation()
document.getElementById("test").addEventListener('click', first);
function first(e){
e.stopImmediatePropagation();
this.removeEventListener("click", first);
document.onclick = second;
}
function second(){
alert("I'm not suppose to appear after the first click, only the second.");
}Run Code Online (Sandbox Code Playgroud)
<button type="button" id="test">Click</button>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10359 次 |
| 最近记录: |