Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IE不将链接标记为;参观;当通过javascript呈现时_Javascript_Ajax_Internet Explorer_Hashchange - Fatal编程技术网

IE不将链接标记为;参观;当通过javascript呈现时

IE不将链接标记为;参观;当通过javascript呈现时,javascript,ajax,internet-explorer,hashchange,Javascript,Ajax,Internet Explorer,Hashchange,我正在使用一个站点,其中所有内容都是使用jquery通过ajax回发呈现的。我正在使用Ben Alman的hashchange()来管理哈希历史记录,它允许我为页面添加书签,使用后退按钮等等。。。除了IE 9之外,一切都很完美。在IE中,存在一个小问题,即“已访问”链接未标记为已访问。您可以看到,在加载新内容之前,单击链接后,链接会在一瞬间变成紫色(已访问)。但是,一旦单击“后退”按钮,链接就会显示为从未访问过。下面是我所说的JFIDLE示例: 以下是JSFIDLE代码,假设您在head中引用

我正在使用一个站点,其中所有内容都是使用jquery通过ajax回发呈现的。我正在使用Ben Alman的hashchange()来管理哈希历史记录,它允许我为页面添加书签,使用后退按钮等等。。。除了IE 9之外,一切都很完美。在IE中,存在一个小问题,即“已访问”链接未标记为已访问。您可以看到,在加载新内容之前,单击链接后,链接会在一瞬间变成紫色(已访问)。但是,一旦单击“后退”按钮,链接就会显示为从未访问过。下面是我所说的JFIDLE示例:

以下是JSFIDLE代码,假设您在head中引用了jquery和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(); 
});
$(函数(){
//将事件绑定到window.onhashchange,当哈希更改时,该事件将获取
//散列并将类“selected”添加到任何匹配的导航链接。
$(窗口).hashchange(函数(){
警报(“哈希更改为:“+location.Hash”);
var hash=location.hash;
//根据散列设置页面标题。
document.title='哈希为'+(hash.replace(/^#/,'')| |'blank')+';
//模拟由ajax回调呈现的实体
如果(哈希==“”){
$(“body”).html(“”;
}
否则{
$(“body”).html(“在此窗格中单击鼠标右键并选择“后退”);
}
})
//由于事件仅在哈希更改时触发,因此我们需要触发
//事件,以处理页面可能已加载的哈希。
$(window.hashchange();
});

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

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;}
HTML如果IE
jqueryjsif-IE
$(函数(){
$(a)。单击(函数(){
$(this.attr('id').addClass('visted_link_class');
});
});
CSS
.访问链接类{颜色:#您的颜色;}

这是ie中的一项安全功能。在许多现代浏览器中,:visted的功能受到限制,以防止CSS攻击。
因此,这个问题没有解决方法。

如果您在将DOM段附加到文档之前创建适当的元素并构建DOM段,则可能会出现这种情况

不确定它是否会工作,不能在这里测试它,但我尝试调整您的代码

$(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")
            .append($("<a>")
              .attr("href","#test1")
              .text("teste 1"))
            .append($("<a>")
              .attr("href","#test2")
              .text("test 2"))
            .append($("<a>")
              .attr("href","#test3")
              .text("test 3"))
        );
      }
      else{
        $("body").text("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(); 
});
$(函数(){
//将事件绑定到window.onhashchange,当哈希更改时,该事件将获取
//散列并将类“selected”添加到任何匹配的导航链接。
$(窗口).hashchange(函数(){
警报(“哈希更改为:“+location.Hash”);
var hash=location.hash;
//根据散列设置页面标题。
document.title='哈希为'+(hash.replace(/^#/,'')| |'blank')+';
//模拟由ajax回调呈现的实体
如果(哈希==“”){
$(“body”).html(
$(“”).id(“nav”)
.append($(“”)
.attr(“href”,“#test1”)
.文本(“测试1”))
.append($(“”)
.attr(“href”,“测试2”)
.文本(“测试2”))
.append($(“”)
.attr(“href”,“#test3”)
.文本(“测试3”))
);
}
否则{
$(“正文”).text(“在此窗格中单击鼠标右键并选择“返回”);
}
})
//由于事件仅在哈希更改时触发,因此我们需要触发
//事件,以处理页面可能已加载的哈希。
$(window.hashchange();
});

尝试考虑CSS LVHA角色,这意味着A标签伪类的顺序很重要。p> 第一次定义这些类:

  • A:链接
  • A:参观了
  • A:悬停
  • A:主动的
如果这仍然不能解决您的问题,您可以使用另一个js路由器(hashchange):
我经常使用这个,它在很多情况下都能完美工作。

一个选项是使用HTML5历史API来伪造浏览器历史。这样,只有在删除浏览器历史记录后,链接才会“未访问”

就像我说的:

上面的[…]方法使用 “/hello”,尽管未请求任何资产,且窗口仍处于打开状态 同一页。然而,这里有一个问题。在击中背部时 按钮,我们会发现我们没有返回到本文的URL,但 相反,我们将回到以前的页面。这是 因为replaceState不操纵浏览器的历史记录,所以它 只需替换地址栏中的当前URL即可

因此,正如该页中提到的,您必须执行以下操作:

您只需使用IE条件注释即可加载特定样式:

您可以简单地使用IE条件注释加载特定样式:

<!--[if IE]>
  a:visited {
     padding-left: 8px;
     background: url(images/checkmark.gif) no-repeat scroll 0 0;
}
<![endif]-->


IE可能需要链接的有效目标(例如带有正确名称的
标记)。我在这里没有IE测试,所以这只是一个猜测。看起来jQuery也遇到了这个问题:看起来他们没有解决它。我相信它在IE 10上可以工作。问题似乎出在IE9和IE8上。我使用了一点javascript,将所有单击的URL保存到本地存储,并在每次ajax回发后通过jquery对本地存储中找到的页面上的任何链接应用css规则进行迭代。这确实是一个黑客程序,但它有效。你试过使用吗?它允许您以编程方式将项目推送到浏览器的历史记录中。它支持IE9。可能是您的vlink与未访问链接的颜色相同。我使用了类似的解决方法,但不是将访问状态存储在隐藏输入中,而是将它们存储在本地存储中。不确定您在本地存储中投入了多少,但如果遇到性能问题,您可能也想检查一下这一点:您描述的漏洞是您
 You can simply use IE conditional comments to load a specific style:    
<!--[if IE]>
      a:visited {
         padding-left: 8px;
         background: url(images/checkmark.gif) no-repeat scroll 0 0;
    }
<![endif]-->
<!--[if IE]>
  a:visited {
     padding-left: 8px;
     background: url(images/checkmark.gif) no-repeat scroll 0 0;
}
<![endif]-->