如何让Anchor Links在Jquery Mobile中运行?

Jquery Mobile决定将锚链接视为排序的页面请求。 但是,如果您有大量博客post具有指向同一页面的锚点链接(即href =“#specs”),那么这并不好。

有没有办法在特定页面上禁用jquery mobile的锚链接使用,我知道我不会在它上面使用它,所以我可以按照预期使用锚链接,下拉到页面的一部分?

我只需要在同一页面上的锚链接解决方案(即:href =“#specs”)。

谢谢

您可以尝试在锚标记上添加data-ajax="false"

没有Ajax的链接

指向其他域或具有rel =“external”,data-ajax =“false”或目标属性的链接将不会加载Ajax。 相反,这些链接将导致整页刷新而没有动画过渡。 两个属性(rel =“external”和data-ajax =“false”)具有相同的效果,但是在链接到另一个站点或域时应使用不同的语义:rel =“external”,而data-ajax =“ false“对于简单地选择域中的页面通过Ajax加载非常有用。 由于安全性限制,框架始终选择从Ajax行为中链接到外部域。

参考 – http://jquerymobile.com/demos/1.0.1/docs/pages/page-links.html

如果您像我一样,转换现有网站并且您现在不想浏览每个页面。 您可以在标题中添加一行代码,并且所有标题和所有现有内部锚链接都将添加data-ajax =“false”标记。

当然,这假设您已经在标题中包含您自己的javascript文件。 如果你不是,你无论如何都要触摸每一页。 但是我已经有一个包含在每个页面中的单个javascript文件,所以我添加了这一行……

 $("a").each(function () { if(this.href.indexOf("#")>=0) $(this).attr("data-ajax",false); }); 

这将在你的$(document).ready()块中。 如果你还没有那个区块,这里就是整个区块。

 $(document).ready(function() { $("a").each(function () { if(this.href.indexOf("#")>=0) $(this).attr("data-ajax",false); }); }); 

希望这可以帮助。 这是user700284提供的相同解决方案,但是以自动方式提供。

您可以将以下代码添加到页面的末尾:

   

并将类“native-anchor”添加到您的锚链接。

它不是一个完全的解决方案,因为浏览器的后退按钮会将您移动到上一页而不是链接的位置,但它比不起作用的链接更好。

我在这里发现了这个解决方案: jQuery Mobile Anchor Linking

 $(document).bind("mobileinit", function () { $.mobile.ajaxEnabled = false; }); 

首先,您必须将此代码放入custom.js文件中

 $(document).bind('mobileinit', function () { $.mobile.loader.prototype.options.disabled = true; $.mobile.ajaxEnabled = false; $.mobile.linkBindingEnabled = false; $.mobile.loadingMessage = false; }); 

然后在加载jquery mobile js之前将此文件添加到您的网页中。 因为'mobilinit'事件立即被触发

谢谢你这个解决方案对我有用

  

我用index.php替换了# ,这是我的文档根目录。 但是,它不适用于表单按钮,即input type="submit"

 // On page load on mobiles only, look for the specific a tag you want to take control over, // alternatively you can still target all 'a' tags $('a[href*="#component"]').each(function () { // then set data-ajax to false, $(this).attr("data-ajax", false); // at this point you can add the class to your target a tags. // You can do it elsewhere but because for this example my // 'a' tags are automatically generated so I just add the class here $(this).addClass('in-pagelink'); // then target the class and bind to a click event $('a.in-pagelink').bind('click', function (ev) { // here I redirect the page with window.location.assign // as opposed to window.location.href. I find that it works better window.location.assign(this.href); // then I close my navigation menu closeAll(); }); });