Jquery: mark links as visited without opening them

2019-05-13 16:07发布

I have no intention of just altering the link (I hear that's impossible, but if it's not I'd love to know how). I'm fine with adding it to the the browser history if that needs to be done.

I'd like to loop through all <a>'s on a page and change their state to visited. For example:

$("a").each(function(){
   //mark as visited (somehow?)
});

Essentially creating a "Mark All as Read" button on my page. Any ideas?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-05-13 16:55

It's not really possible in the way you want however you could copy the CSS attributes of a:visited to a new class called "visited" or whatever then apply them to the links you want.

$('a').addClass('visited');
查看更多
Animai°情兽
3楼-- · 2019-05-13 17:05

You could ...

1) Try using AJAX (ie. $.get(a.href)), but I don't know if that would actually work.

2) Try styling the links to look visited (by changing their CSS "color" attribute), although this approach won't actually make them in to visited links

3) If neither of those work, you could try doing something like:

3A) Create a hidden IFRAME on the page

3B) Add a target attribute to every A tag on the page to make them point to the hidden IFRAME

3C) Invoke $("a").click() inside that each loop (possibly with a delay of some sort between each one to give the page time to load)

If none of those work, I think you're out of luck.

查看更多
叼着烟拽天下
4楼-- · 2019-05-13 17:09

Create a link that is visited (i.e. set its href to window.location), and set each of your target links' styles to that newly created link's computed style.

查看更多
登录 后发表回答