2010-09-01 106 views
1

基本上,如果點擊特定的<a>並將其傳遞給另一個方法,我希望獲得'URLValue'。還有其他一些帶有class =「LinkClass」的<a>元素,我寫了一個JQuery來只獲得點擊的元素值。 下面是一個可用的JQuery來做這件事,它引用了XSL。點擊鏈接觸發函數調用,返回點擊的鏈接值

$("a.LinkClass").live("click", function() { 
     var URL = $(this).attr("href"); 
     //Now call another method passing this value 
    }); 

但是,我可以直接通過XSL使用該值,觸發事件點擊鏈接的函數調用嗎?

XSL如下:

<a class="LinkClass"> 
     <xsl:attribute name="href"> 
     <xsl:value-of select="URLValue"/> 
     </xsl:attribute> 
    </a> 
+0

你可以靜態定義'a/@ onclick'。如果這不是你所要求的,那麼請注意XSLT在任何用戶迭代之前都可以工作。 – 2010-09-01 18:28:10

+0

我一直在試圖做到這一點。不起作用。不知道我錯過了什麼。 試圖通過用簡單的javascript方法寫出HTML來簡化它。 並在做 Page1 不起作用。 – 2010-09-01 19:28:32

+0

然後,您嘗試的內容超出了XSLT的範圍。靜態定義onclick屬性可以在XSLT中完成。爲什麼這不適用於你的情況可能是因爲很多原因。我很想知道你已經完成了轉型。 – 2010-09-01 19:41:57

回答

1

當此XML文檔是瀏覽器中打開:

<?xml-stylesheet type="text/xsl" href="test.xsl"?> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Test XSLT javascript injektion</title> 
    </head> 
    <body> 
     <h2>Test XSLT javascript injektion</h2> 
     <ul> 
      <li><a href="http://www.google.com">Google</a></li> 
      <li><a href="http://www.stackoverflow.com">Stack Overflow</a></li> 
     </ul> 
    </body> 
</html> 

而這個樣式表作爲 「test.xsl」:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xhtml="http://www.w3.org/1999/xhtml"> 
    <xsl:output method="xml" omit-xml-declaration="yes" 
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/> 
    <xsl:template match="processing-instruction()" priority="1"/> 
    <xsl:template match="node()|@*" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="xhtml:a/node()[1]"> 
     <xsl:attribute name="onclick"> 
      <xsl:value-of select='concat("alert(&apos;",..,"&apos;)")'/> 
     </xsl:attribute> 
     <xsl:call-template name="identity"/> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Test XSLT javascript injektion</title> 
    </head> 
    <body> 
     <h2>Test XSLT javascript injektion</h2> 
     <ul> 
      <li> 
       <a href="http://www.google.com" onclick="alert('Google')">Google</a> 
      </li> 
      <li> 
       <a href="http://www.stackoverflow.com" onclick="alert('Stack Overflow')">Stack Overflow</a> 
      </li> 
     </ul> 
    </body> 
</html> 

並且警報在點擊時起作用。

+0

謝謝。有效! – 2010-09-02 14:44:52

+0

@Amy:你很好! – 2010-09-02 14:54:45

+0

實際上XSL不工作:(輸出頁面正在工作讓我再試一次 – 2010-09-02 15:21:30