无法访问JavaScript中xsl中存在的Html标记(Unable to access Html tags present within xsl in JavaScript)

编程入门 行业动态 更新时间:2024-10-13 16:21:27
无法访问JavaScript中xsl中存在的Html标记(Unable to access Html tags present within xsl in JavaScript)

我有一个使用XML和XSL的html页面。 还有像<table> , <tr>和<td>这样的html标签。 我想从javaScript访问这些标记,并从javaScript为其属性设置值。 我尝试使用GetElementById,GetElementByName,GetElementByTagName访问下面发布的代码,但无法这样做。

代码摘录:

<xml id="xmlSchedule" LANGUAGE=javascript onreadystatechange="return xmlSchedule_onreadystatechange()"></xml> <xml id="xslSchedule"> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template> <table id="tblSchedule" index='0' class="GridText" style="TABLE-LAYOUT:fixed;FONT-SIZE:9pt;FONT-FAMILY:verdana;width=100%"> <xsl:for-each select="VOE-OBJECT/ITEM"> <tr id="trSchedule" onmouseover="this.style.cursor='default'"> <xsl:attribute name="TWRow"></xsl:attribute> <td data="25" width="20" height='17' align='left'> <img><xsl:attribute name="SRC"><xsl:eval>getChargeIcon(this)</xsl:eval></xsl:attribute><xsl:attribute name="title"><xsl:eval>getChargeTitle(this)</xsl:eval></xsl:attribute> </img> </td> <td data="24" id='tdNote' width="20" height="17" align='middle'> <img><xsl:attribute name="SRC"><xsl:eval>getNoteIcon(this)</xsl:eval></xsl:attribute><xsl:attribute name="title"><xsl:eval>getNoteTitle(this)</xsl:eval></xsl:attribute> </img> </td>

JavaScript功能:

function XYZ() { var oRow = document.GetElementByName("TWRow"); var oLength = oRow.childNodes.length; for (var i = 0; i < olength; i++) { oRow.childNodes.item(i).attributes.getNamedItem("data")= i; }

当我使用document.GetElementByName(“TWRow”)并快速检查时,它返回了一个对象,但count为0。

我搜索了很多但找不到相关的东西。 我是非常新的XML和XSL请指导。

I have an html page which uses XML and XSL. And there are html tags like <table>, <tr>, and <td>. I want to access these tags from javaScript and set value for its attributes from javaScript. I tried using GetElementById, GetElementByName,GetElementByTagName to access the and in the code posted below but was unable to do so.

Excerpts of the code:

<xml id="xmlSchedule" LANGUAGE=javascript onreadystatechange="return xmlSchedule_onreadystatechange()"></xml> <xml id="xslSchedule"> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template> <table id="tblSchedule" index='0' class="GridText" style="TABLE-LAYOUT:fixed;FONT-SIZE:9pt;FONT-FAMILY:verdana;width=100%"> <xsl:for-each select="VOE-OBJECT/ITEM"> <tr id="trSchedule" onmouseover="this.style.cursor='default'"> <xsl:attribute name="TWRow"></xsl:attribute> <td data="25" width="20" height='17' align='left'> <img><xsl:attribute name="SRC"><xsl:eval>getChargeIcon(this)</xsl:eval></xsl:attribute><xsl:attribute name="title"><xsl:eval>getChargeTitle(this)</xsl:eval></xsl:attribute> </img> </td> <td data="24" id='tdNote' width="20" height="17" align='middle'> <img><xsl:attribute name="SRC"><xsl:eval>getNoteIcon(this)</xsl:eval></xsl:attribute><xsl:attribute name="title"><xsl:eval>getNoteTitle(this)</xsl:eval></xsl:attribute> </img> </td>

JavaScript Function:

function XYZ() { var oRow = document.GetElementByName("TWRow"); var oLength = oRow.childNodes.length; for (var i = 0; i < olength; i++) { oRow.childNodes.item(i).attributes.getNamedItem("data")= i; }

And when I used document.GetElementByName("TWRow") and check in quick watch it returned a object but count was 0.

I searched a lot but couldn't find anything relevant. I am very new to XML and XSL please guide.

最满意答案

首先,我相信你应该在这里使用“GetElementsByName”,而不是“GetElementByName”(因为它对多个同名的元素有效)

var oRow = document.GetElementByName("TWRow");

但它找不到任何东西的原因是因为HTML中没有名称为“TWRow”的元素。 你不这样做的原因是因为这个XSLT

<tr id="trSchedule" onmouseover="this.style.cursor='default'"> <xsl:attribute name="TWRow"></xsl:attribute>

这实际上是在tr元素上创建一个名为TWRow的属性

<tr id="trSchedule" onmouseover="this.style.cursor='default'" TWRow="">

但是要使GetElementsByName起作用,它需要看起来像这样

<tr id="trSchedule" onmouseover="this.style.cursor='default'" name="TWRow">

要解决此问题,请将XSLT更改为:

<tr id="trSchedule" onmouseover="this.style.cursor='default'"> <xsl:attribute name="name">TWRow</xsl:attribute>

或者更好的是,直接用你的XSLT写出属性

<tr id="trSchedule" onmouseover="this.style.cursor='default'" name="TWRow">

Firstly, I believe you should be using "GetElementsByName" here, not "GetElementByName" (because it is valid for multiple elements the same name)

var oRow = document.GetElementByName("TWRow");

But the reason it is not finding anything is because you have no elements in your HTML with the name "TWRow". And the reason you don't is because of this XSLT

<tr id="trSchedule" onmouseover="this.style.cursor='default'"> <xsl:attribute name="TWRow"></xsl:attribute>

This is actually creating an attribute called TWRow on your tr element

<tr id="trSchedule" onmouseover="this.style.cursor='default'" TWRow="">

But for GetElementsByName to work, it needs to look like this

<tr id="trSchedule" onmouseover="this.style.cursor='default'" name="TWRow">

To fix this, change the XSLT to as follows:

<tr id="trSchedule" onmouseover="this.style.cursor='default'"> <xsl:attribute name="name">TWRow</xsl:attribute>

Or better still, just write out the attribute directly un your XSLT

<tr id="trSchedule" onmouseover="this.style.cursor='default'" name="TWRow">

更多推荐

id,GetElementByName,<xsl,电脑培训,计算机培训,IT培训"/> <meta name="d

本文发布于:2023-08-01 13:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   无法访问   xsl   JavaScript   present

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!