Javascript 如果存在警报语句,代码为何工作
我有以下代码:Javascript 如果存在警报语句,代码为何工作,javascript,jquery,Javascript,Jquery,我有以下代码: $("#doctorLists").click(function(){ $("#doctorListsDisplay").slideToggle("slow", function() { if ( $('#doctorListsDisplay').is(':hidden')) { //alert ('hidden'); $('#searchMP').css({ top: $("#doctorLists").p
$("#doctorLists").click(function(){
$("#doctorListsDisplay").slideToggle("slow", function() {
if ( $('#doctorListsDisplay').is(':hidden')) {
//alert ('hidden');
$('#searchMP').css({ top: $("#doctorLists").position().top + 40 }).show();
if ($('#searchMPDisplay').is(':visible')) {
alert("MP visible");
$('#searchMPDisplay').css({ top: $("#searchMP").position().top + 30 }).show();
}
value +=180;
$('#imgArrowDown').rotate({ animateTo:value})
}
else {
//alert ('shown');
$('#searchMP').css({ top: $("#doctorLists").position().top + 230 }).show();
if ($('#searchMPDisplay').is(':visible')) {
alert("MP hidden");
$('#searchMPDisplay').css({ top: $("#searchMP").position().top + 30 }).show();
}
value +=180;
$('#imgArrowDown').rotate({ animateTo:value})
}
});
});
以下代码行唯一起作用的时间是警报语句未被注释掉:
if ($('#searchMPDisplay').is(':visible')) {
alert("visible");
$('#searchMPDisplay').css({ top: $("#searchMP").position().top + 30 }).show();
}
及
我添加了alert语句以查看状态,但现在如果删除它,代码将不起作用。有人能帮我找出原因吗
可能的解释是,警报会挂起代码执行,直到您试图显示/隐藏的元素添加到DOM中
调试代码的最佳方法是添加
console.log
而不是警报,其中的信息是DOM中已经存在元素。更改结果的警报表示某些异步因素正在影响代码,例如动画或ajax请求。请不要使用警报来调试代码,请使用控制台。这就是为什么。可能重复的我有一个滑块也显示在页面中。那么,克服这个问题的最佳方法是什么呢?首先,找出问题所在。在这一点上,除了基于动画或ajax调用之外,我们没有其他想法。或者setTimeout。详细说明“代码不工作”为什么代码不工作?那么我该如何解决这个问题?我将等待document ready事件,或者如果元素不在DOM中,则使用setTimeout。你也可以看看这里
if ($('#searchMPDisplay').is(':visible')) {
alert("hidden");
$('#searchMPDisplay').css({ top: $("#searchMP").position().top + 30 }).show();
}