当通过javascript呈现时,IE不将链接标记为"已访问"

Tri*_*ter 21 javascript ajax internet-explorer hashchange

我正在使用一个网站,其中所有内容都是使用jquery通过ajax回发呈现的.我正在使用Ben Alman的hashchange(http://benalman.com/projects/jquery-hashchange-plugin/)来管理哈希历史记录,它允许我为页面添加书签,使用后退按钮等等...一切都可以完美地应用于所有内容IE 9当然.在IE中存在一个小问题,"访问"链接未标记为已访问.在加载新内容之前单击链接后,您可以看到该链接在一瞬间变为紫色(已访问).但是,一旦单击后退按钮,链接就会显示为从未访问过.以下是我所谈论的一个例子:http: //jsfiddle.net/7nj3x/3/

这里是jsfiddle代码,假设你有jquery和head中引用的hashchange插件:

$(function(){
  // Bind an event to window.onhashchange that, when the hash changes, gets the
  // hash and adds the class "selected" to any matching nav link.
  $(window).hashchange( function(){
    alert("Hash changed to:"+location.hash);  
    var hash = location.hash;
    // Set the page title based on the hash.
    document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';
    //simulate body being rendered by ajax callback 
      if(hash == ""){  
        $("body").html("<p id='nav'><a href='#test1'>test 1</a> <a href='#test2'>test 2</a> <a href='#test3'>test 3</a></p>");
      }
      else{
        $("body").html("Right click within this pane and select \"Back\".");  
      }
  })
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).hashchange(); 
});
Run Code Online (Sandbox Code Playgroud)

小智 0

为什么不设置一个仅供 IE 使用的代码块来设置隐藏输入标记的值以反映点击行为。如果单击链接,您可以将输入标记的值设置为等于该链接 id,并允许您更新元素类以反映更改。

HTML if IE
<input type="hidden" id="clicked_link" />


JQuery JS if IE
$(function() {
    $(a).click(function() {
        $(this).attr('id').addClass('visited_link_class');
    });
});

CSS
.visited_link_class { color:#your color;}
Run Code Online (Sandbox Code Playgroud)