2013-02-05 98 views
0

我有一個ASP搜索腳本,它返回的結果主要是PDF和一些其他文檔。在該站點的大部分中,我使用名爲clueTip的jQuery插件(由Karl Swedberg創建),當用戶將鼠標懸停在某些鏈接上時創建工具提示。這些鏈接的內容是通過AJAX從鏈接的rel屬性定義的HTML文件拉:如何添加基於href文件名的rel屬性

<a href="../../example.pdf" class="tips" rel="../../tooltips/example.html> 

當用戶執行搜索我希望能夠動態地這些提示從一個特定添加到PDF文件位置 - ../../technicalarticles。所以,我需要:

  1. 找到任何返回在../../technicalarticles,並添加class="tips"到鏈接
  2. 從那些我需要(從最後/一切文件擴展名)提取相關文件的名稱PDF文件的PDF文件
  3. 追加該文件名的rel屬性

下面是我的jQuery代碼至今:

<script type="text/javascript"> 
$(".results a").each(function() { 
    if (window.location.href.indexOf("technicalarticles") >= 0) 
    // if statement to find the right pdfs 
    { 
     var firstpos = location.href.lastIndexOf('/')+1; // finds the position of the last/and adds 1 
     var lastpos = location.href.lastIndexOf('.')-1; // finds the position of the last . and subtracts 1 
     var filename = location.href.substr(firstpos, lastpos); // filename should now have eveything from the last/to the file extension 

     $(this).attr('rel', ".data/tooltips/" + filename + ".html"); // adds the rel string to the link 
     $(this).addClass('tips'); // adds the class 'tips' to the link 
    } 
}); 
</script> 

回答

0

window.location是當前頁面的url。您需要使用jQuery方法的上下文$(this) - 當前已處理的鏈接。嘗試獲取/設置它通過.attr(屬性)這樣

var target = $(this); 
someUrlPath = /here get part of url/ 
target.attr('rel', target.attr('rel') + someUrlPath); 

但我認爲一個更好的做法是使用ASP填充相對參數,如果可能的。