此链接准确显示了您正在寻找的内容:http: //jmperezperez.com/prevent-iphone-navigation-bar-ajax-link-click/。
演示(下面的代码)的唯一问题是它#
为每个链接添加了一个哈希,从而破坏了导航。如果有人知道解决方法,我将不胜感激。
HTML:
<ul>
<li>
<a class="prevent" href="/">
This link is ignored. However, it shows the bar
</a>
</li>
<li>
<a class="facebook" href="/blog">
This link is ignored. It does not show the bar,
and modifies the hash tag.
</a>
</li>
</ul>
JS:
//hide bar on page load
setTimeout(function () { window.scrollTo(0, 1);}, 500);
//attach event touchend
document.addEventListener('touchend',
function(e) {
var target = e.target;
while(target.nodeName !== 'A' && target.nodeName !== 'BODY') {
target = target.parentNode;
}
if (target.nodeName === 'A' &&
target.className === 'facebook') {
target.href = '#!' + target.getAttribute('href');
}
},
false
);
//attach event click
document.addEventListener('click',
function(e) {
if (e.target.nodeName === 'A') {
if (e.target.className === 'prevent') {
e.preventDefault();
} else if (e.target.className === 'facebook') {
var href = e.target.getAttribute('href');
if (href.indexOf('#!') === 0) {
var newHref = href.substr(2);
e.target.href = newHref;
location.hash = newHref;
e.preventDefault();
}
}
}
},
false
);