Javascript e、 链接上的preventDefault()仍会加载页面
标题可能有点误导,但我不知道如何更好地表达它,所以我为此道歉 我正在创建一个自定义处理程序,以便在按下新内容时网站不会刷新(例如,类似于youtube的工作方式) 为此,我使用以下脚本:Javascript e、 链接上的preventDefault()仍会加载页面,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,标题可能有点误导,但我不知道如何更好地表达它,所以我为此道歉 我正在创建一个自定义处理程序,以便在按下新内容时网站不会刷新(例如,类似于youtube的工作方式) 为此,我使用以下脚本: $('.sidebar2 li a').click(function (e) { test = true; var button = $(this); var noteId = button.data("noteid"); $(".sidebar2 li.active").re
$('.sidebar2 li a').click(function (e) {
test = true;
var button = $(this);
var noteId = button.data("noteid");
$(".sidebar2 li.active").removeClass("active");
var postData = { id: noteId };
$.ajax({
url: '/API/Note',
type: 'get',
data: postData,
success: function (resp) {
if (resp.success == true) {
$('#app-bar-left').html(resp.note.navBarHTML);
$('#cell-content').html(resp.note.noteContentHTML);
window.history.pushState({ path: window.location.href }, resp.note.title, '/MyNotes/Note/' + resp.note.noteId);
document.title = resp.note.title;
$('*[data-noteId="'+resp.note.noteId+'"]').parent().addClass("active")
e.preventDefault();
test = false;
return false;
}
}
});
});
尽管我已经声明了要触发的e.preventDefault()
,javascript将新内容加载到当前帧中而不刷新,但浏览器仍然会再次刷新
我曾尝试使用href=“#”
但是在这种情况下,当我回去处理时,我总是会得到两个相同的页面,一个没有,另一个在最后有#,此外,拥有所有链接href=“#”
即使我告诉他没有,我让浏览器“正常”重定向有什么错
我还尝试在
a
元素上添加onclick=“javascript:void(0)”
,但没有任何帮助ajax
是异步的。在调用成功回调时,事件将已经从DOM树中冒泡出来并进行处理。在发送请求之前,您需要调用preventDefault
$('.sidebar2 li a').click(function (e) {
e.preventDefault(); // here for example
test = true;
var button = $(this);
var noteId = button.data("noteid");
$(".sidebar2 li.active").removeClass("active");
var postData = { id: noteId };
$.ajax({
url: '/API/Note',
type: 'get',
data: postData,
success: function (resp) {
if (resp.success == true) {
$('#app-bar-left').html(resp.note.navBarHTML);
$('#cell-content').html(resp.note.noteContentHTML);
window.history.pushState({ path: window.location.href }, resp.note.title, '/MyNotes/Note/' + resp.note.noteId);
document.title = resp.note.title;
$('*[data-noteId="'+resp.note.noteId+'"]').parent().addClass("active")
test = false;
// returning here makes no sense also
// return false;
}
}
});
});
ajax
是异步的。在调用成功回调时,事件将已经从DOM树中冒泡出来并进行处理。在发送请求之前,您需要调用preventDefault
$('.sidebar2 li a').click(function (e) {
e.preventDefault(); // here for example
test = true;
var button = $(this);
var noteId = button.data("noteid");
$(".sidebar2 li.active").removeClass("active");
var postData = { id: noteId };
$.ajax({
url: '/API/Note',
type: 'get',
data: postData,
success: function (resp) {
if (resp.success == true) {
$('#app-bar-left').html(resp.note.navBarHTML);
$('#cell-content').html(resp.note.noteContentHTML);
window.history.pushState({ path: window.location.href }, resp.note.title, '/MyNotes/Note/' + resp.note.noteId);
document.title = resp.note.title;
$('*[data-noteId="'+resp.note.noteId+'"]').parent().addClass("active")
test = false;
// returning here makes no sense also
// return false;
}
}
});
});
类似于
return
内部success
。首先,它无处可去,即使它回来了,也会回来得太晚。谢谢你,尤里,这正是我想要的。我的想法是在success中添加preventDefault,以便在ajax出现故障或其他情况时可以返回到正常加载,我不认为这会导致其他问题。你知道我该怎么做吗?(我的意思是,如果ajax失败,网站将加载“default ly”)@DethoRhyne您可以添加错误
回调,并根据您的要求手动更改window.location.href
或window.location.hash
。类似于返回内成功
。首先,它无处可去,即使它回来了,也会回来得太晚。谢谢你,尤里,这正是我想要的。我的想法是在success中添加preventDefault,以便在ajax出现故障或其他情况时可以返回到正常加载,我不认为这会导致其他问题。你知道我该怎么做吗?(我的意思是,如果ajax失败,网站将加载“default ly”)@DethoRhyne您可以添加错误
回调,并根据您的要求手动更改window.location.href
或window.location.hash
。