更新Chrome浏览器中href属性时访问链接的访问状态 [英] Updating visited status of a link when changing its href attribute, under Chrome

查看:271
本文介绍了更新Chrome浏览器中href属性时访问链接的访问状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用JavaScript更新链接时

  $('#link_id')。attr('href',some_new_url) 

访问/未访问链接的颜色主题会持续存在,无论新url地址的状态如何。

有没有办法改变链接地址强制浏览器重新检查其访问状态?



更进一步笔记:

我(在OSX 10.8上)在Chrome(32)和Safari(6.1)中遇到此问题。在Firefox(26)中,链接状态根据需要自动更新。

上面的例子在jQuery中,但问题与使用vanilla JavaScript相同 document.getElementById setAttribute



如果可能的话,请避免删除并添加< a>< / a> 。)编辑:



最小(非)工作示例(由Joeytje50提供): http ://jsfiddle.net/3pdVW/

解决方案

权威答案



你可以做什么来解决这个问题,只需要强制浏览器通过完全删除 href 属性重新计算样式,然后再次重新添加它之后立即。这将使浏览器首先计算样式,因为< a> 不再是没有 href ,然后添加你想给它的 href 。代码会是:

  $('#link_id')。removeAttr('href')。prop('href' ,some_new_url); 

.attr('href')



以前的尝试



什么我在想这是一个渲染故障。当:visited 状态由于脚本而改变时,它不会重新计算样式。 这个问题的最小例子很好地展示了这一点。你可以尝试的是以下任何一种:



使用元素的属性



问题可能是你正在改变属性,这反过来改变了href属性。如果您直接更改href属性,它可能会工作。您可以通过 jQuery.prop 执行此操作,该代码将使用以下代码更改链接:

  $('#link_id')。prop('href',some_new_url); 

演示 。我对这个没有太高的期望,但值得尝试。我怀疑的工作会更好:

强制重新计算样式



如果您想强制重新计算样式,您可以简单地更改要更新的最高元素上的类。由于您单独更新< a> 元素,因此您需要如下所示的内容:

<$ p $ ('href',some_new_url).toggleClass('webkit-force-recalculate'); $('#c $ c> $('#link_id')。

演示



我认为这很有可能会成功。






如果这两种方法都不适合您,您当然可以使用 maximglad​​kov的解决方案


When I am updating links with JavaScript

$('#link_id').attr('href', some_new_url)

the color theme for visited/non-visited links persists, regardless of the status of the new url address.

Is there a way to change link address forcing browser to re-check its visited status?

Further notes:

I am (on OSX 10.8) experiencing this problem in Chrome (32) and Safari (6.1). In Firefox (26) the links status gets updated automatically, as desired.

The example above is in jQuery, but the problem is the same way with with vanilla JavaScript, i.e. document.getElementById and setAttribute.

(I would prefer to avoid deleting and adding <a></a>, if possible.)

EDIT:

Minimal (non-)working example (by Joeytje50): http://jsfiddle.net/3pdVW/

解决方案

Definitive answer

What you could do to fix this, is simply forcing the browser to recalculate the styles by completely removing the href attribute, and then re-adding it back again immediately afterwards. That will make the browser first calculate the styles, since the <a> is no longer a link without the href, and then you add the href you want to give it. The code would then be:

$('#link_id').removeAttr('href').prop('href', some_new_url);

Demo

PS: in this case, using .attr('href', some_new_url); would probably work equally fine.

Previous attempts

What I'm thinking is that this is a rendering glitch. It doesn't recalculate the styles when the :visited state changes because of the script. This minimal example of your problem shows this well. What you could try is either of the following:

Using the element's properties

What the problem might be is that you're changing the attribute, which in turn changes the href property. If you directly change the href property it might work. You can do this via jQuery.prop, which would use the following code to change the link:

$('#link_id').prop('href', some_new_url);

Demo. I don't really have very high hopes about this one, but it's worth trying. What I do suspect will work much better is:

Forcing to recalculate the styles

If you want to force a recalculation of the styles, you can simply change a class on the highest element you want updated. Since you're updating the <a> element alone, you'd need something like this:

$('#link_id').prop('href', some_new_url).toggleClass('webkit-force-recalculate');

Demo

I think that is quite likely to do the trick.


If neither of these approaches work for you, you could of course use maximgladkov's solution.

这篇关于更新Chrome浏览器中href属性时访问链接的访问状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆