开发者

JQuery focus() on jQuery-applied anchors works in Chrome and IE, not Firefox

开发者 https://www.devze.com 2022-12-15 09:09 出处:网络
Update: This question has been solved, so the buggy Firefox behavior will no longer appear when loading my example webpage

Update: This question has been solved, so the buggy Firefox behavior will no longer appear when loading my example webpage

My webpage has very long passages of text, so I want to make each paragraph its own permalink. To do this, I use jQuery to add an anchor and link to each paragraph after the page loads. This worked perfectly in Chrome. To get this to work in IE I added an explicit jQuery focus() function after applying the anchors. However, this does not work in Firefox if I either load the page for the first time or refresh it (e.g. http开发者_JAVA百科://www.readsherlock.com/study.php#p4). The relevant javascript is pasted below.

 $(document).ready(function() {
    var i = 1;
    $("#text p").each(function(i){
        $(this).html('<a name="p' + i + '" href="#p' + i++ + '">' + $(this).html() + '</a>');
    });
    if(location.hash != '') 
        $(location.hash).focus();
  });

Thanks!


This works for me in Firefox 3.0.16

$(document).ready(function() {
    var i = 1;
    $("#text p").each(function(i){
        $(this).html('<a name="p' + i + '" href="#p' + i++ + '">' + $(this).html() + '</a>');
    });
    if(location.hash != '') 
        location.hash = location.hash;
});

I haven't checked any other browsers so you many need to do both the focus and the reset to trigger all browsers.

0

精彩评论

暂无评论...
验证码 换一张
取 消