简体   繁体   中英

Change the value of select attribute of xsl:for-each tag by javascript

I want to change value of select in xsl:for-each

<script>
var val = someValue() ://String
document.getElementById("test").select=val;
</script>
<body>
<xsl:for-each id="test" select="get from JS" >
.... 
.....
</xsl:for-each>
</body>

It seems like you want to change the value of an attribute of a DOM node. You can use setAttribute("attribute", "value") to achieve that.

document.getElementById("test").setAttribute("select", val);

You may refer DOM API reference here: https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

Typically when running XSLT in the browser, the XSLT code is first executed to generate the HTML page, and only when the HTML page has been generated are JS scripts within the page executed. By then it's too late to change the XSLT (it's already been executed).

Perhaps if you explain what you are actually trying to achieve then we can suggest an alternative approach.

You might like to take a look at Saxon-JS which provides a more dynamic way of exploiting XSLT in the browser, with more opportunity for the XSLT and Javascript code to work together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM