我在为广播电台构建的 Webflow 站点上遇到了一个奇怪的问题,并且在任何地方都找不到解决方案,所以我想我会尝试向您寻求帮助。
我有这个 ajax 页面加载器代码,允许在浏览网站时连续播放无线电流。该代码可以工作,除非它在单击任何内容时会破坏所有其他自定义代码。控制台中没有错误,我尝试在不同的代码编辑器之间移动代码,但问题仍然存在。我不知道代码是否以某种方式与 Webflow 发生冲突,但代码是纯香草 javascript,所以这个问题让我发疯。
如果有人可能知道问题所在,请随时分享您的解决方案。我真的很感激!这是ajax代码:
let main = document.querySelector('main');
window.addEventListener('popstate', function (e) {
requestPage(e.state, false);
});
function requestPage(url, push) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
main.innerHTML = xhr.responseText;
var title = (/<title>(.*?)<\/title>/m).exec(xhr.responseText)[1];
var headers = document.querySelectorAll('.header');
var footers = document.querySelectorAll('.footer');
if (headers.length > 1) {
headers[1].remove();
};
if (footers.length > 1) {
footers[1].remove();
};
attachLinkClickHandlers(main);
document.title = title;
if (push)
history.pushState(url, document.title, url);
}
};
xhr.open('get', url, true);
xhr.send();
}
function attachLinkClickHandlers(parent) {
let links = parent.querySelectorAll('a:not([href^="http"])');
[].forEach.call(links, function (link) {
link.addEventListener('click', function (e) {
requestPage(link.href, true);
e.preventDefault();
return false;
});
});
}
attachLinkClickHandlers(document);
history.replaceState(location.href, document.title, location.href);