简体   繁体   English

是否有可能从javascript获取锚访问状态?

[英]is it possible to get anchor visited state from javascript?

  1. i'm using jquery. 我正在使用jquery。 i have a anchor list. 我有一个主播名单。 i'm enumerate anchors, if it visited, set display:none; 我枚举锚点,如果它访问过,则设置display:none;
  2. i need when click on the anchor, anchor will changed to visited state from javascript? 我需要单击锚点时,锚点将从javascript变为访问状态?

How can i do? 我能怎么做?

Yeah, see here for an example. 是的,请参见此处的示例。 It uses getComputedStyle to find out if a link has been visited. 它使用getComputedStyle来确定是否已访问链接。 There's also a variant of this hack that doesn't require scripting . 该hack也有一个变种, 不需要脚本

The relevant part of the example is this (modified for clarity): 该示例的相关部分是这样(为清楚起见进行了修改):

a:visited {
    color: #00f;
}

var link = document.createElement('a');
link.href = 'http://example.com/';
document.body.appendChild(link);
var color = document.defaultView.getComputedStyle(link, null).getPropertyValue('color');       
// check for visited
if (color == "rgb(0, 0, 255)") {           
    alert(link.href + ' has been visited');
}

May I ask what do you need it for? 请问您需要什么?

Edit: WRT #2, you could open the link in an iframe . 编辑:WRT#2,您可以在iframe打开链接。 That would mark it as visited in the browser history. 它将在浏览器历史记录中将其标记为已访问。 Like so: 像这样:

var iframe = document.createElement('iframe');
iframe.src = 'http://example.com/';
document.body.appendChild(iframe);

Edit : You can create new CSS rules with JS. 编辑 :您可以使用JS创建新的CSS规则。 There's a jQuery plugin to make it more simple. 有一个jQuery插件使其更简单。 Basically, you would do it like this: 基本上,您可以这样做:

$.rule('a:visited { color: #f06 !important }').appendTo('style');

通过CSS怎么做?

a:visited {display:none;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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