问题描述
|
我试图将xsl变量值传递给javascript函数。
我的xsl变量
<xsl:variable name=\"title\" select=\"TITLE\" />
我正在传递这样的价值
<input type=\"button\" value=\"view\" onclick=\"javascript:openPage(\'review.html?review=$title\')\" />
我已经以不同可能的方式尝试了上面的代码,但是我遇到了错误。
<script type=\"text/javascript\">
function jsV() {
var jsVar = \'<xsl:value-of select=\"TITLE\"/>\';
return jsVar;
}
</script>
<input type=\"button\" value=\"view\" onclick=\"javascript:openPage(\'javascript:jsV()\')\" />
I also tried
<input type=\"button\" value=\"view\" onclick=\"javascript:openPage(\'review.html?review=\'\\\'\'
+$title+\'\\\')\" />
有替代方法还是我做的不正确?
解决方法
您忘记了{}:
<input type=\"button\" value=\"view\" onclick=\"javascript:openPage(\'review.html?review={$title}\')\" />
, 这是一个有效的示例,如何执行此操作:
此转换:
<xsl:stylesheet version=\"1.0\"
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
<xsl:output omit-xml-declaration=\"yes\" indent=\"yes\"/>
<xsl:strip-space elements=\"*\"/>
<xsl:template match=\"/*\">
<xsl:variable name=\"vTitle\" select=\"TITLE\"/>
<input type=\"button\" value=\"view\"
onclick=\"javascript:openPage(\'review.html?review={$vTitle}\')\" />
</xsl:template>
</xsl:stylesheet>
应用于此XML文档时(未提供XML文档!):
<contents>
<TITLE>I am a title</TITLE>
</contents>
产生想要的正确结果:
<input type=\"button\" value=\"view\"
onclick=\"javascript:openPage(\'review.html?review=I am a title\')\"/>
说明:使用AVT(属性值模板)。
, 通过执行以下操作,也可以从同一文件中的JavaScript代码访问xsl变量:
<xsl:variable name=\"title\" select=\"TITLE\"/>
<script type=\"text/javascript\">
function getTitle() {
var title = <xsl:value-of select=\"$title\"/>;
return title;
}
</script>