How to get Anchor Links to work in Jquery Mobile?

后端 未结 7 2285
情书的邮戳
情书的邮戳 2020-12-08 20:47

Jquery Mobile has decided to treat anchor links as page requests of sort. However, this isn\'t good if you have a load of blog posts which have anchor links to the same page

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 21:02

    If you are like me, converting an existing site and you don't want to go through every page right now. You can add one line of code to your header and all of your header and all of your existing internal anchor links will get the data-ajax="false" tag added.

    Of course, this assumes you are including your own javascript file up in the header already. If you are not you would have to touch every page anyway. But I have a single javascript file that is included in every page already so I added this line...

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

    This goes in your $(document).ready() block. If you don't have that block yet, here is the entire block.

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

    Hope this helps. It is the same solution user700284 offers but in an automated way.

提交回复
热议问题