1

XML データ ファイルを参照する VBS スクリプト ファイルを作成しました。XML データ ファイル内に、必要な HTML データが埋め込まれています。

<![CDATA[]'other interesting HTML data here'].

XPATH を使用してこの HTML データを取り除き、変数として表される Div オブジェクト (myDiv) 要素に挿入しました (ドキュメントには書き込まれません)。たとえば、myDiv.innerHTML の内容は次のようになります。

<table> 
<tr><td>text in cell 1</td></tr> 
<tr><td><h1 id="myId1">my text for H1</h></td><tr> 
<tr><td><h2 id="myId2">my text for h2</h></td></tr> 
</table> 

最初にやりたいことは、「myId1」に一致する ID を持つ適切なタグを選択することです。したがって、次のようなステートメントを使用しました。

MyIdText = MyDiv.getElementById("myId1") 

ただし、私が使用しているアプリケーションには、「Err 438、オブジェクトはこのプロパティまたはメソッドをサポートしていません」と表示されます。私はコードの初心者で、基本的な基本事項のいくつかを理解できますが、もう少し複雑になると少し迷子になります (申し訳ありません)。私はこの掲示板の他の投稿を調べましたが、それらはすべて VBScript ではなく、HTML と Javascript に関連しているようです (私が使用しているアプリケーションは Java Script を許可しません)。コードを間違って使用していますか?

4

2 に答える 2

0

getElementById() を使用するには、document.getElementById("myId1") と記述する必要があります。このようにして、指定された ID を「ドキュメント」内で検索するようにブラウザに指示します。変数が定義されておらず、このメソッドがアタッチされていないため、コードで上記のエラーが生成されます。

特定の H 要素内のテキストを抽出するには:

MyIdText = document.getElementById("myId1").textContent;

于 2012-12-03T11:48:11.443 に答える
0

助けてくれてありがとう、残念ながら、私は少し VBS を知っており、DOM についても少ししか知らないので、実験して両方を学ぼうとしています。私が使用している環境/アプリケーション内には特定の制限があります (ASCE と呼ばれ、セーフティ ケースを管理するためのツールですが、現時点では重要ではありません)。ただし、リンゴとリンゴを比較するために、DOM/VBS コマンドが実際に実行できることをよりよく理解できるように、HTML ページ内で実験を試みました。私は部分的に成功しましたが、なぜそれがどこに落ちるのかまだ理解できません。これが私が実験している正確なファイルです。各セクションにコメントテキストを追加しました。

<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>
于 2012-12-05T09:43:18.247 に答える