2009-07-31 98 views
8

我想通過jQuery選擇所有訪問的鏈接。這裏是使用jQuery選擇訪問的鏈接

<div class="question-summary"> 
    <a class="question-hyperlink">Stuff</a> 
</div> 

如果question-hyperlink已被訪問的HTML,我是選擇question-summary。有任何想法嗎?

回答

8

下面是選擇:

$("a:visited").parent(".question-summary") 

用例:

$("a:visited").parent(".question-summary").addClass("is_visited"); 
28

我想這應該提到的是給定的方法已經在出於安全原因,瀏覽器被禁用。由於可以通過檢查訪問的鏈接來檢索訪問者的歷史記錄,已採取了某些措施來防止這種情況。

來源:Mozilla Foundation Blog.

經過在Chrome和FF - 兩者都不支持$("a:visited")任何更長的時間。

2

我發現基於解決方法上的localStorage上Nevyan's Blog: Mark visited links using JavaScript and localStorage

他提出了乾淨的JavaScript代碼來存儲頁面的用戶在localStorage的被點擊的鏈接,並添加類「參觀」到<a>元素的父:

function check_visited_links() { 
    var visited_links = JSON.parse(localStorage.getItem('visited_links')) || []; 
    var links = document.getElementsByTagName('a'); 
    for (var i = 0; i < links.length; i++) { 
     var that = links[i]; 
     that.onclick = function() { 
      var clicked_url = this.href; 
      if (visited_links.indexOf(clicked_url) == -1) { 
       visited_links.push(clicked_url); 
       localStorage.setItem('visited_links', JSON.stringify(visited_links)); 
      } 
     } 
     if (visited_links.indexOf(that.href) !== -1) { 
      that.parentNode.className += ' visited'; 
     } 
    } 
} 

我不知道它是否比被訪問的方法更安全。

+0

這種看起來像一個新的問題。此外,您將希望在此放置相關代碼和文檔以支持您的答案,而不是解答的鏈接。他們可以改變,影響你正在回答的有效性。 – rfornal 2015-02-16 16:12:08

0

在此處輸入代碼`它不支持JavaScript,因爲我也嘗試查找方法來收集訪問的鏈接數據以隱藏訪問的節點。

一些參考: 隱私和:訪問選擇器 - CSS | MDN

如果你只關心樣式,你應該可以通過CSS實現它,但通過屏幕上顯示的內容應該是觀察它被訪問的唯一方法。

我在Greasemonkey的userscript中這樣做,讓那些沒有:visited風格的網站顯示那些已經訪問過的鏈接。

// ==UserScript== 
// @description ADD a:visited for CSS 
// @include  *annalscts.com* 
// @include  *thejns.org* 
// @include  *turkishneurosurgery.org.tr* 
// @include  *nature.com* 
// @include  *academic.oup.com* 
// @include  *sagepub.com* 
// @grant   GM_addStyle 
// ==/UserScript== 
GM_addStyle('a:visited {color:#EE5665 !important}'); 

爲了收集數據,以本地我使用的Greasemonkey API

GM_setValue 
GM_getValue 

我只是看着教程在Youtube上的API,並嘗試寫入到userscript

的Greasemonkey API:值只是在Youtube上搜索這個標題。

書面指導:http://nulleffort.com/greasemonkey-api-values/

Greasemonkey的文檔:https://wiki.greasespot.net/Greasemonkey_Manual:API

我userscript的某些部分

//Firstly, var the ordinary variable preVisitedLinks and assigning to memory variable visitedLinks (At first the value should be undefined) 
var preVisitedLinks = GM_getValue("visitedLinks"); 
unsafeWindow.aclick = function(tlink){ 
    window.open(tlink, '_blank', 'toolbar=yes,scrollbars=yes,resizable=yes,top=10,left=10,width=10,height=10'); // click a button added and get the link visited in my script 
    //If the ordinary variable preVisitedLinks is undefined (First time running the script) 
    if(preVisitedLinks.includes('undefined')){ 
     GM_setValue('preVisitedLinks', '|' + tlink.replace('http://paper.pubmed.cn/','')); 
    } 
    //If the ordinary variable preVisitedLinks is not undefined, continue to add each new string collect 
    else{ 
     GM_setValue('preVisitedLinks', preVisitedLinks + '|' + tlink.replace('http://paper.pubmed.cn/','')); 
    } 
    //The ordinary variable preVisitedLinks assigning to memory variable visitedLinks value. The magic is the variable name the same. 
    preVisitedLinks = GM_getValue("preVisitedLinks"); 
    if(preVisitedLinks.length > 27500){ 
     preVisitedLinks = preVisitedLinks.substr(preVisitedLinks.length - 27500); 
    } 
    //The memory variable visitedLinks value assigning to the ordinary variable preVisitedLinks value 
    GM_setValue('visitedLinks',preVisitedLinks); 
    console.info(preVisitedLinks); 
}; 

而在一些地方,我使用字符串來檢測訪問過的鏈接代碼

if(preVisitedLinks.includes(trs[i].querySelectorAll('li')[0].querySelector('a').href.replace('http://xxx.xxxx.com/',''))){ 
     trs[i].remove(); 
    }