[英]How to pass javascript variable to xsl variable?
之前曾有人問過這個問題,但我想評估一下這是否可能?
有沒有一種簡單的方法可以將javascript變量傳遞給xsl變量? 原因是,該變量將來自外部腳本。
這是我的無效代碼...
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<div>
<script type="text/javascript">
var key = window.location.href;
</script>
<xsl:variable name="jsvar">$key</xsl:variable>
<xsl:value-of select="$jsvar"/>
</div>
</xsl:template>
我想在網頁上顯示“ Term1”。
任何想法?
XSL正在輸出script
標記,它不是定義在XSL上下文中可執行的任何內容的東西。 您可以做的是在全局范圍內定義XSL變量,然后在腳本和div中重新使用它:
<xsl:variable name="jsvar">Term1</xsl:variable>
<xsl:template match="/">
<script>
var key = "<xsl:value-of select="$jsvar"/>";
</script>
<div>
<xsl:value-of select="$jsvar"/>
</div>
</xsl:template>
如果要在執行JS時讓JS知道一個變量,並對其進行某種處理,例如將其顯示在瀏覽器頁面的元素中,則必須在JS中執行此操作:
<xsl:template match="/">
<script>
window.onload = function() {
document.getElementById('location').textContent = window.location.href;
};
</script>
<div id="location"></div>
</xsl:template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.