简体   繁体   中英

Anchor Tags not working in Chrome

I'm using anchor tags in my script so that when I go back to the page I can save the position I was at. The problem is it only works when I click the link. When I refresh the page, or go back to it it doesn't. What am I not seeing? Believe it or not... this is a problem with Chrome - it actually works fine in IE!!!(I can't believe I just said that)

function parseXML(xml)
{
    //find every Category and print the title
    var output = '';    
    var ms = 0;
    $(xml).find("category").each(function()
    {   

        output += '<h3>' +$(this).find("title").text() + '<a name="m' + (ms+1) + '"></a> </h3> ' ;
        var div = '<div>';
        output += '<ul>';   
        $(this).find('items > item').each(function() {
            var text = $(this).find("text").text();
            var slink = $(this).find("link").text();
            output += "<li class='subLink' src='"+ slink + "'><a href='#m"+ms+"'>"  + text + "</a></li>";

        });

        output += '</ul>';      
        ms++;
    });         
    var icons = {
  header: "ui-icon-circle-arrow-e",
  activeHeader: "ui-icon-circle-arrow-s"
};
    var hashNum = 0;
    if (window.location.hash != ''){
        hashNum = parseInt(window.location.hash.replace("#m", "")); 

    };
$('<div>')
.attr('id','accordionSub')
.html(output)       
.appendTo('#accordionSubB').delay(1).queue(function(){
    $( "#accordionSub" ).accordion({
                heightStyle: "content",
                collapsible: true,
                icons: icons,
                active: hashNum
            });
});

}

OUTPUT:

<h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-accordion-icons ui-state-hover"
role="tab" id="ui-accordion-accordionSub-header-22" aria-controls="ui-accordion-accordionSub-panel-22"
aria-selected="false" tabindex="-1">
    <span class="ui-accordion-header-icon ui-icon ui-icon-circle-arrow-e"></span>Pressure Transducers
    <a name="m23"></a>
</h3>

Use id instead name in your anchor.

HTML Links - The id Attribute The id attribute can be used to create a bookmark inside an HTML document.

Tip: Bookmarks are not displayed in any special way. They are invisible to the reader.

Example An anchor with an id inside an HTML document:

<a id="tips">Useful Tips Section</a>

Create a link to the "Useful Tips Section" inside the same document:

<a href="#tips">Visit the Useful Tips Section</a>

Or, create a link to the "Useful Tips Section" from another page:

<a href="http://www.w3schools.com/html_links.htm#tips">

Visit the Useful Tips Section

http://www.w3schools.com/html/html_links.asp

Found a solution I had to add this:

$(window).load(function(){
    var hashNum = 0;
    if (window.location.hash != ''){
        hashNum = window.location.hash.replace("#m", "");   
        console.log('hashNum: ' + hashNum); 
    };


    hashMenu = $("#m"+hashNum-1).offset().top;

      $('html,body').animate({
          scrollTop: hashMenu
    }, 1000);

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM