我正在寻找关于如何禁用链接的最佳实践,该链接将重定向到与jquery/ jquerymobile相同的页面。
例如,如果我在主页上,有一个指向主页的链接,它甚至不应该刷新页面。
我的链接代码:
<a href="Homepage.aspx" rel="external">Home</a>发布于 2013-01-18 08:46:20
我对jquery/jquery mobile还不够熟悉,不知道这是否是最佳实践,但它应该可以工作。
$("a").each(function(){
if(window.location.href==this.href)
this.onclick=function(){return false};
});发布于 2013-01-18 08:45:54
完全未经测试,但这取决于您的代码是如何编写的。
$('a').each(function(){
if($(this).attr('href') === window.location.pathname)
$(this).removeAttr('href');
// Alternatively: $(this).attr('href','#');
});发布于 2013-01-18 08:49:45
您可以使用window.location.href解析当前的URL并提取页面名称。
然后,可以对每个链接进行迭代,并附加与当前页面值具有相同href值的链接的行为
var currentPage = ....// extract the page from window.location.href
$('a').each(function(index, element) {
var currentA = $(this);
if (currentA.attr('href') == currentPage) {
// Append a listener on the click event that will return false
// and stop the default behaviour
currentA.on('click', false);
}
});注意:将false设置为处理函数与创建返回false的匿名函数相同,也与创建调用event.stopPropoagation()和event.preventDefault()的函数相同
https://stackoverflow.com/questions/14390666
复制相似问题