Html 使用GetElementById从特定元素提取文本

Html 使用GetElementById从特定元素提取文本,html,dom,vbscript,Html,Dom,Vbscript,我创建了一个查看XML数据文件的VBS脚本文件。 在XML数据文件中,我需要的HTML数据嵌入到 <![CDATA[]'other interesting HTML data here']. 但是,我正在使用的应用程序表示err438,对象不支持此属性或方法。 我是一个代码新手,能够理解一些基本的基本语法,但是当它变得有点复杂时,我会有点迷路。我已经浏览了这个板块上的其他帖子,它们似乎都是HTML和Javascript,而不是VBScript。我正在使用的应用程序将不允许使用Java脚本

我创建了一个查看XML数据文件的VBS脚本文件。 在XML数据文件中,我需要的HTML数据嵌入到

<![CDATA[]'other interesting HTML data here'].
但是,我正在使用的应用程序表示err438,对象不支持此属性或方法。 我是一个代码新手,能够理解一些基本的基本语法,但是当它变得有点复杂时,我会有点迷路。我已经浏览了这个板块上的其他帖子,它们似乎都是HTML和Javascript,而不是VBScript。我正在使用的应用程序将不允许使用Java脚本。
我使用的代码是否错误?

要使用getElementById,您应该编写:document.getElementByIdmyId1。通过这种方式,您可以告诉浏览器在“文档”中搜索指定的ID。您的变量未定义,并且没有附加此方法,因此您的代码将生成上述错误

要提取特定H元素内的文本,请执行以下操作:

MyIdText=document.getElementByIdmyId1.textContent;

非常感谢您的帮助,不幸的是,我对VBS有一点了解,甚至对DOM知之甚少,我正在尝试通过实验来学习这两者。我正在使用的环境/应用程序(称为ASCE)及其管理安全案例的工具中存在某些限制,但目前这并不重要。 然而,为了比较苹果与苹果,我尝试在HTML页面中进行实验,以便更好地理解DOM/VBS命令的实际功能。我已经取得了一些部分的成功,但仍然不明白为什么会失败。 这是我正在试验的确切文件,我为每个部分添加了注释文本

<html>
<head>

<table border=1>
    <tr>
        <td>text in cell 1</td>
    </tr>

    <tr>
        <td><h1 id="myId1">my text for H1</h1></td>
    </tr>

    <tr>
        <td><h1 id="myId2">my text for h2</h2></td>
    </tr>
</table>

<script type="text/vbscript">
DoStuff

Sub DoStuff 

    ' Section 1: Get a node with the Id value of "myId1" from the above HTML
    ' and assign it to the variable 'GetValue'
    ' This works fine :-)
    Dim GetValue
    GetValue = document.getElementById("myId1").innerHTML
    MsgBox "the text=" & GetValue   

    ' Section 2: Create a query that assigs to the variable 'MyH1Tags' to  all of the <h1> 
    ' tags in the document.
    ' I assumed that this would be a 'collection of <h1> tags so I set up a loop to itterate
    ' through however many there were, but this fails as the browser says that this object 
    ' doesn't support this property or method - This is where I am stuck    

    Dim MyH1Tags    
    Dim H1Tag
    MyH1Tags = document.getElementsByTagName("h1") ' this works

    For Each H1Tag in MyH1Tags ' this is where it falls over
        MSgbox "Hello"
    Next


    ' Section 3: Create a new Div element 'NewDiv' and then insert some HTML 'MyHTML'
    ' into 'NewDiv'. Create a query 'MyHeadings' that extracts all h1 headings from 'NewDiv'
    ' then loop round for however many h1 headings there are in 'MyHeadings'
    ' and display the text content. This works Ok

    Dim NewDiv  
    Dim MyHTML  
    Dim MyHeadings
    Dim MyHeading
    Set NewDiv = document.createElement("DIV")      
    MyHTML="<h1 id=""a"">heading1</h1><h2 id=""b"">Heading2</h2>"  
    NewDiv.innerHTML=MyHTML
    Set MyHeadings = NewDiv.getElementsByTagName("h1")

    For Each MyHeading in MyHeadings
        Msgbox "MyHeading=" & MyHeading.innerHTML
    Next


    'Section 4: Do a combination of Section 1 (that works) and Section 3 (that works)
    ' by creating a new Div element 'NewDiv2' and then paste into it some HTML
    ' 'MyHTML2' and then attempt to create a query that extracts  the inner HTML from 
    ' an id attribute with the value of "a". But this doesnt work either.
    ' I have tried "Set MyId = NewDiv2.getElementById("a").innerHTML" and
    ' also tried "Set MyId = NewDiv2.getElementById("a")" and it always falls over 
    ' at the same line.
    Dim NewDiv2 
    Dim MyHTML2     
    Dim MyId

    Set NewDiv2 = document.createElement("DIV")     
    MyHTML2="<h1 id=""a"">heading1</h1><h2 id=""b"">Heading2</h2>"  
    NewDiv2.innerHTML=MyHTML

    MyId = NewDiv2.getElementById("a").innerHTML

End Sub

</script>
</head>

<body>

我已经从XML文件中提取了html,创建了一个元素,并将html插入到元素中。html实际上并不存在于文档中,它存在于MyDiv元素变量中。我能够对MyDiv元素执行GetElementsByTagName操作,但GetElementById操作似乎不起作用。我也在使用VBS而不是JavaScript…我将尝试并完成它
MyIdText = MyDiv.getElementById("myId1") 
<html>
<head>

<table border=1>
    <tr>
        <td>text in cell 1</td>
    </tr>

    <tr>
        <td><h1 id="myId1">my text for H1</h1></td>
    </tr>

    <tr>
        <td><h1 id="myId2">my text for h2</h2></td>
    </tr>
</table>

<script type="text/vbscript">
DoStuff

Sub DoStuff 

    ' Section 1: Get a node with the Id value of "myId1" from the above HTML
    ' and assign it to the variable 'GetValue'
    ' This works fine :-)
    Dim GetValue
    GetValue = document.getElementById("myId1").innerHTML
    MsgBox "the text=" & GetValue   

    ' Section 2: Create a query that assigs to the variable 'MyH1Tags' to  all of the <h1> 
    ' tags in the document.
    ' I assumed that this would be a 'collection of <h1> tags so I set up a loop to itterate
    ' through however many there were, but this fails as the browser says that this object 
    ' doesn't support this property or method - This is where I am stuck    

    Dim MyH1Tags    
    Dim H1Tag
    MyH1Tags = document.getElementsByTagName("h1") ' this works

    For Each H1Tag in MyH1Tags ' this is where it falls over
        MSgbox "Hello"
    Next


    ' Section 3: Create a new Div element 'NewDiv' and then insert some HTML 'MyHTML'
    ' into 'NewDiv'. Create a query 'MyHeadings' that extracts all h1 headings from 'NewDiv'
    ' then loop round for however many h1 headings there are in 'MyHeadings'
    ' and display the text content. This works Ok

    Dim NewDiv  
    Dim MyHTML  
    Dim MyHeadings
    Dim MyHeading
    Set NewDiv = document.createElement("DIV")      
    MyHTML="<h1 id=""a"">heading1</h1><h2 id=""b"">Heading2</h2>"  
    NewDiv.innerHTML=MyHTML
    Set MyHeadings = NewDiv.getElementsByTagName("h1")

    For Each MyHeading in MyHeadings
        Msgbox "MyHeading=" & MyHeading.innerHTML
    Next


    'Section 4: Do a combination of Section 1 (that works) and Section 3 (that works)
    ' by creating a new Div element 'NewDiv2' and then paste into it some HTML
    ' 'MyHTML2' and then attempt to create a query that extracts  the inner HTML from 
    ' an id attribute with the value of "a". But this doesnt work either.
    ' I have tried "Set MyId = NewDiv2.getElementById("a").innerHTML" and
    ' also tried "Set MyId = NewDiv2.getElementById("a")" and it always falls over 
    ' at the same line.
    Dim NewDiv2 
    Dim MyHTML2     
    Dim MyId

    Set NewDiv2 = document.createElement("DIV")     
    MyHTML2="<h1 id=""a"">heading1</h1><h2 id=""b"">Heading2</h2>"  
    NewDiv2.innerHTML=MyHTML

    MyId = NewDiv2.getElementById("a").innerHTML

End Sub

</script>
</head>

<body>