使用 jQuery 完成单独函数时执行警报

perform alert when seperate function is done using jquery

本文关键字:执行 函数 单独 jQuery 使用      更新时间:2023-09-26

我在页面中有一个函数。我想在 changecolor() 完成操作时执行警报。但问题是由于某些限制,我无法更改 changecolor()。所以无论如何都知道功能已经完成。我附上了演示小提琴

function changecolor(id){
$(id).css('backgroundColor','green')
}
changecolor().done(function(){
alert(0)
})
如果你想在

完成另一个函数后触发另一个函数,你可以在 when/then 使用。同样在您的原始小提琴中,您不会将参数传递给 changecolor() 函数

function changecolor(id){
$.when($(id).css('backgroundColor','green')).then(alert('0'));
}

小提琴

好的试试

这个

<div style="width:200px; height:200px; background:#000" onclick="changecolor(this); alert(0);"></div>

或者将两个函数包装在单个方法中

你好朋友你可以这样做...

function changecolor(id){
$(id).css('backgroundColor','green')
}
function CallChangeColor(id){
        changecolor(id);
    alert(0);
}
<div style="width:200px; height:200px; background:#000" onclick="CallChangeColor(this);"></div>

或者你可以做这样的事情...

function changecolor(id){
$(id).css('backgroundColor','green')
}
<div style="width:200px; height:200px; background:#000" onclick="changecolor(this);alert(0);"></div>