Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

jquery - is it possible to get anchor visited state from javascript?

  1. i'm using jquery. i have a anchor list. i'm enumerate anchors, if it visited, set display:none;
  2. i need when click on the anchor, anchor will changed to visited state from javascript?

How can i do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yeah, see here for an example. It uses getComputedStyle to find out if a link has been visited. There's also a variant of this hack that doesn't require scripting.

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. 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. There's a jQuery plugin to make it more simple. Basically, you would do it like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...