XSLT:在元素标记中输出Javascript
XSLT: Output Javascript in the element tag
使用XSLT,如何输出以下
<div onclick="var e = document.getElementById('<xsl:value-of select="div_id"/>');
if(e.style.display == 'block')
e.style.display = 'none';
else
{
e.style.display = 'block';
e.scrollIntoView();
}"
style="text-decoration: underline; color: blue;"
>Toggle</div>
注意:代码应该位于onclick
属性中,我只能访问文档的正文。
一种方法是使用AVT(属性值模板):
<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="/">
<div style="text-decoration: underline; color: blue;"
onclick="var e = document.getElementById('{div_id}');
if(e.style.display == 'block')
e.style.display = 'none';
else
{{
e.style.display = 'block';
e.scrollIntoView();
}}">Toggle</div>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下XML文档时:
<div_id>3</div_id>
生成所需结果:
<div style="text-decoration: underline; color: blue;" onclick="var e = document.getElementById('3'); if(e.style.display == 'block') e.style.display = 'none'; else { e.style.display = 'block'; e.scrollIntoView(); }">Toggle</div>
请注意:指定此类AVT(属性值模板)时,必须将必须生成的任何{
或}
字符加倍。
另一种方法是使用xsl:attribute
:
<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="/">
<div style="text-decoration: underline; color: blue;">
<xsl:attribute name="onclick">
var e = document.getElementById('<xsl:value-of select="div_id"/>');
if(e.style.display == 'block')
e.style.display = 'none';
else
{
e.style.display = 'block';
e.scrollIntoView();
}</xsl:attribute>Toggle</div>
</xsl:template>
</xsl:stylesheet>
我认为您想要使用属性值模板,例如
<div onclick="var e = document.getElementById('{div_id}'); ...">Toggle</div>
相关文章:
- 以字符串形式输出Javascript中的日期
- 使用PHP输出JavaScript时会出现神秘的换行符
- 如何从PHP文件输出javascript文件,该文件可以从脚本包含标记中引用
- XSLT:在元素标记中输出Javascript
- 如何在gump工作流中同时在独立文件和依赖文件中输出javascript
- 当从代码后台输出Javascript时,如何处理换行符
- 以XSL输出Javascript文档.write
- PHP 文件输出 javascript 代码本身而不是运行
- 为什么我得到未定义的输出Javascript
- ASP.Net 4.0 中,IE 11 中未输出 JavaScript
- Maven GWT - 仅输出 Javascript 文件
- 在不同的变量上输出 JavaScript 函数
- 如何在 html 中输出 javascript 变量的值
- 如何输出JavaScript对象中的元素
- 显示三个文本框之和的输出-Javascript
- jQuery函数中正确输出JavaScript变量
- 构建,修改,输出Javascript数组
- 使用Haxe输出JavaScript
- Javascript和HTML,不输出Javascript函数
- Php _gettext输出javascript代码