是否有可能从JavaScript获取锚访问状态? [英] is it possible to get anchor visited state from javascript?
问题描述
- 我正在使用jquery。我有一个锚定列表。我枚举锚点,如果它访问,设置显示:无;
-
- i'm using jquery. i have a anchor list. i'm enumerate anchors, if it visited, set display:none;
- i need when click on the anchor, anchor will changed to visited state from javascript?
我需要点击锚点时,锚点将会改变为JavaScript的访问状态吗? >我该怎么办?
是的,请参阅这里举例说明。它使用 getComputedStyle
来确定链接是否已被访问。 不需要脚本编写也有这种黑客的变种。 示例的相关部分是这样(为了清晰起见而修改):
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');
//检查访问
if(color ==rgb(0,0,255)){
alert(link.href +'has visited');
}
请问您需要什么?
编辑:WRT#2,您可以在 iframe
中打开链接。这将标记为在浏览器历史记录中被访问。像这样:
var iframe = document.createElement('iframe');
iframe.src ='http://example.com/';
document.body.appendChild(iframe);
编辑:您可以使用JS创建新的CSS规则。有一个 jQuery插件使其更简单。基本上,你会这样做:
$。rule('a:visited {color:#f06!important}' ).appendTo( '风格');
How can i do?
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');
这篇关于是否有可能从JavaScript获取锚访问状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!