簡體   English   中英

如何將 xsl 變量值傳遞給 javascript function

[英]how to pass a xsl variable value to a javascript function

我正在嘗試將 xsl 變量值傳遞給 javascript function。

我的 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM