Javascript 从嵌套回调返回假/停止传播
我以前见过有人问过这个问题,但在我自己的工作中没有实现解决方案。页面继续跳转到顶部 首先,我有一个听众:Javascript 从嵌套回调返回假/停止传播,javascript,callback,jquery-callback,Javascript,Callback,Jquery Callback,我以前见过有人问过这个问题,但在我自己的工作中没有实现解决方案。页面继续跳转到顶部 首先,我有一个听众: $('.edit-description').click(function(e) { e.stopPropagation(); contentContainer = $('#description'); openEditor(contentContainer); return false; }); 然后使用包含回调的openEditor函数: function op
$('.edit-description').click(function(e) {
e.stopPropagation();
contentContainer = $('#description');
openEditor(contentContainer);
return false;
});
然后使用包含回调的openEditor函数:
function openEditor(contentContainer){
var contentType=contentContainer.data('value');
getRecentEdits(markid, entityType, contentType, function(data){
if (editArea){
hiddenContentArea.show();
editArea.hide();
}
hiddenContentArea=contentContainer;
contentContainer.hide();
editArea = $('<div class="edit-container" style="padding-bottom: 10px;">' +
' <textarea class="form-control js-auto-size" style="margin-bottom:5px;">'+ data[0].content+
' </textarea>' +
' <a class="right leave-editor"><i class="fa fa-times-circle"></i>Leave Editor</a>'+
' <button type="button" class="btn btn-sm btn-custom-green submit-edit pull-right right " style="color:#fff;padding-top:5px; background-color: #3D8A11; border: 1px solid #f2f2f2;"><i class="fa fa-check-circle"></i>Submit</button>'+
'</div>');
for(i = 0; i < data.length; i++){
/** var fileLink = '<a href="#">'+data[i].username+'</a>';
editArea.prepend(fileLink);**/
console.log(data[i]);
}
var parent=contentContainer.parents("p");
parent.after(editArea);
$('textarea.js-auto-size').textareaAutoSize();
if (contentType==1){
$('.edit-container').prepend('<div style="color:red;">Note: The description area should contain only facts. Opinions and reviews can be posted on the board below.</div>');
}
return false;
});
return false;
}
正如你所看到的,我在所有可能的地方都设置了“返回错误”
对不起,代码太多了。没有看到发布所有内容的任何解决方案
衷心感谢您的帮助。非常感谢 使用
.preventDefault()
而不是stopPropagation()
。单击处理程序中的return false
应该就足够了,并且应该可以工作,除非其中一个被调用函数抛出(考虑到您的代码中完全没有局部变量声明,这不会让我感到惊讶)。但是问题出在哪里?你已经展示了你的解决方案,但我们不知道问题出在哪里。从每个函数返回false
可能不是正确的方法。您是否只是试图阻止单击事件将用户滚动到页面顶部?请在单击处理程序的开头尝试e.preventDefault()
。@Bergi,谢谢。。。抱歉,遗漏了我的变量声明。使用.preventDefault是有效的。我需要检查它和返回false之间的区别。
function getRecentEdits(id, entityType, contentType, callback){
$.ajax({
type: "POST",
url: "get_recent_edits.php",
data: {id: id, entity_type:entityType, content_type:contentType},
dataType: "json",
success: function (data) {
callback(data);
return false;
},
error: function (data) {
console.log(data);
alert("At this time, you could not be added to the list of followers of this location. There was likely a technical difficulty that will fix itself if you try again a bit later. Thanks!");
alert(data['message'], data['success']);
}
});
return false;
}