Trigger a function call on clicking a link, returning the link value that's clicked
Basically, I'm looking to get 'URLValue' if the particular <a>
is clicked and pass it on to another method. There are several other <a>
elements with class="LinkClass" and I have written a JQuery to get only the clicked element value.
Below is a working JQuery to do just this, it references the XSL.
$("a.LinkClass").live("click", function() {
var URL = $(this).attr("href");
//Now call another method passing this 开发者_StackOverflow中文版value
});
However, can I use the value directly through XSL, triggering a function call on event click for the link?
XSL below:
<a class="LinkClass">
<xsl:attribute name="href">
<xsl:value-of select="URLValue"/>
</xsl:attribute>
</a>
When this XML document is open on browser:
<?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>
And this stylesheet as "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('",..,"')")'/>
</xsl:attribute>
<xsl:call-template name="identity"/>
</xsl:template>
</xsl:stylesheet>
Output:
<!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>
And alerts works on click.
精彩评论