2011-03-28 133 views
0

我可以看到螢火蟲我的錨標記的路徑名的值的路徑,但我有一個很難訪問它訪問一個錨標記

<script type="text/javascript"> 
     $(document).ready(function() { 

      $("a").click(function() { 
       alert("Hi"); 

       $.ajax({ 
        type: "POST", 
        url: "NickTracker.asmx/LogActivity", 
        contentType: "application/json; charset=utf-8", 
        data: "{'path': '" + attr('pathname') + "'}", 
        dataType: "json", 
        success: AjaxSucceeded, 
        error: AjaxFailed 
       }); 

      }); 

     }); 
     function AjaxSucceeded(result) { 
      alert(result.d); 
     } 
     function AjaxFailed(result) { 
      alert(result.status + ' ' + result.statusText); 
     } 
    </script> 

這裏是我的html

<body> 
    <form id="form1" runat="server"> 
    <div> 
    Page 1<br /> 
     <br /> 
&nbsp;<a href="http://manual.aspdotnetstorefront.com/p-157-xml-packages.aspx">Manual</a> 
    <br /> 
    <a href="http://www.google.com/">googles</a> 
     <br /> 
     <br /> 
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 
    </div> 
    </form> 
</body> 

當用戶點擊我想要的路徑返回,這並不工作ATTR(「路徑」)

+0

你是什麼意思的「路徑名」,「HREF」屬性? – 2011-03-28 18:26:50

+0

@Pekka我嘗試了href,仍然沒有得到任何東西。我的語法一定是錯的 – 2011-03-28 18:28:12

回答

5

您需要引用this對象和正確的屬性,所以更換attr('pathname')$(this).attr('href'),甚至更簡單this.href

this.pathname的URL主機名,如果你遍歷你的站點根目錄相對的文件這是罰款後只能訪問任何價值,但如果你試圖訪問的文件將打破在不同的主機上。例如,對於href="http://www.google.com/finance"this.pathname將導致剛剛/finance

+1

我不明白他從哪裏得到那個「路徑名」的想法:)它在代碼中沒有地方。 – 2011-03-28 18:29:46

+0

@Pekka〜現在我想知道......它是否在Firebug中說「路徑名」? – 2011-03-28 18:32:11

+0

如果您在「this」節點下的螢火蟲中進行調試,那麼有一個屬性叫做「pathname – 2011-03-28 18:32:55

0

如果你指的是href屬性,甚至一個屬性,叫做路徑,您將需要修復您的ATTR電話。你現在有什麼會尋找一個名爲attr的函數,並嘗試傳遞一個字符串。試試:$(this).attr("href")$(this).attr('pathname')this.attribute("href")。這取決於您是否正在查找添加到名爲pathname或href屬性的錨點的屬性。

0

據稱,所需要的是隻是像

$('a').attr('href'); 

現在,如果您在文檔中有很多不同的<A>標籤,那將是一個問題。你必須用你的意思來區分標籤。通常你用id或class來做。

$('a#myAnchor').attr('href');