是否可能是这样的:
$('#btna').click(function(){
console.log(abc());
})
function abc() {
$('#btnb').click(function(){
var result = false;
});
$('#btnc').click(function(){
var result = true;
});
return result;
}
因此,单击
btna
时,我需要等待单击btnb
或btnc
并将其写入控制台。有什么帮助吗?
最佳答案
其他方法,使用Promises,将按特定顺序运行,以等待上一次单击,但第一个按钮($btna
)除外
const $btna = document.getElementById('btna');
const $btnb = document.getElementById('btnb');
const $btnc = document.getElementById('btnc');
function executeListener(btn ){
return new Promise(function(resolve, reject) {
btn.addEventListener("click" , function(){
let jsonReturn = {
action : "click" ,
button : btn
}
resolve(jsonReturn)
});
})
}
executeListener($btna)
.then(function(btnaSuccess) {
console.log("click btna");
return executeListener($btnb)
}).then(function(btnbSuccess){
console.log("click btnb");
return executeListener($btnc);
}).then(function(btncSuccess){
console.log("click btnc")
console.log("Chispas");
})
<button id="btna">btna</button>
<button id="btnb">btnb</button>
<button id="btnc">btnc</button>
另外,您可以使用Promise.all
const $btna = document.getElementById('btna');
const $btnb = document.getElementById('btnb');
const $btnc = document.getElementById('btnc');
function executeListener(btn ){
return new Promise(function(resolve, reject) {
btn.addEventListener("click" , function(){
let jsonReturn = {
action : "click" ,
button : btn
}
resolve(jsonReturn)
});
})
}
Promise.all([
executeListener($btna),
executeListener($btnb),
executeListener($btnc)])
.then(values => {
console.log("resolve all clicks");
console.log("Chispas")
});
<button id="btna">btna</button>
<button id="btnb">btnb</button>
<button id="btnc">btnc</button>
关于javascript - 获取嵌套点击功能的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50956943/