How to get Anchor Links to work in Jquery Mobile?

后端 未结 7 2266
情书的邮戳
情书的邮戳 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:22

    // 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();
                });
    
            });
    

提交回复
热议问题