在javascript中填充选项列表时遇到问题
我有一个在javascript中填充选项列表时遇到问题,javascript,xml,xslt,xslt-1.0,Javascript,Xml,Xslt,Xslt 1.0,我有一个addPlay(),它是一个按钮,这个过程的一部分是它将填充一个选项列表,但它没有这样做 这是调用它的部分 var gamelogElem=document.getElementById("gamelog"); var newPlay=xmlDoc.createElement("play"); var minIndex=document.getElementById("min").selectedIndex; var secIndex=document.getElementById("
addPlay()
,它是一个按钮,这个过程的一部分是它将填充一个选项列表,但它没有这样做
这是调用它的部分
var gamelogElem=document.getElementById("gamelog");
var newPlay=xmlDoc.createElement("play");
var minIndex=document.getElementById("min").selectedIndex;
var secIndex=document.getElementById("sec").selectedIndex;
var actIndex=document.getElementById("action").selectedIndex;
var playerIndex=document.getElementById("player").selectedIndex;
var minute=document.getElementById("min").options[minIndex].text;
var second=document.getElementById("sec").options[secIndex].text;
var time=minute + ":" + second;
var playerOpt=document.getElementById("player").options;
var player=playerOpt[playerIndex].text;
var op = document.getElementById("player")
.options[document.getElementById("player").selectedIndex];
var optgroup = op.parentNode;
var team=optgroup.label;
var actionOpt=document.getElementById("action").options;
var action=actionOpt[actIndex].text;
// these values above are the ones that should be showing up and are in the play template
newPlay.setAttribute("time", time);
newPlay.setAttribute("player", player);
newPlay.setAttribute("team", team); // can't define
newPlay.setAttribute("action", action);
xmlDoc.documentElement.appendChild(newPlay);
gamelogElem = runTransform(xmlDoc,xsltDoc4);
下面是runTransform
函数
function runTransform(xDoc, xsltDoc) {
var xProcessor = new XSLTProcessor();
xProcessor.importStylesheet(xsltDoc);
var resultDoc = xProcessor.transformToDocument(xDoc);
var serializer = new XMLSerializer();
var resultStr = serializer.serializeToString(resultDoc);
return resultStr;
}
下面是填充模板“play”中选项的部分
显示器的调用是“…”
,您可以在下面看到它,并按照它调用上面设置的值
<xsl:template match="plays">
<table class="actions">
<tr>
<th>Game Log</th>
</tr>
<tr>
<td>
<select name="actionlist" size="20">
<xsl:apply-templates select="play" />
</select>
</td>
</tr>
</table>
</xsl:template>
<xsl:template match="play">
<option>
[<xsl:value-of select="@time" />] <xsl:value-of select="@player" /> (<xsl:value-of select="@team" />) <xsl:value-of select="@action" />
</option>
</xsl:template>
游戏日志
[] ()
更改行xmlDoc.documentElement.appendChild(newPlay);到xmlDoc.getElementsByTagName(“plays”)[0].appendChild(newPlay)
需要在添加新创建的元素标记之前获取它为什么要使用XSLT?您可以在javascript中不使用它来实现这一点:啊,我明白了,
play
元素不是根元素的子元素。根据您的问题,我假设plays
是根元素。