使用外部 vbs 脚本更新 HTA 中 div 的内容

Update content of a div in HTA with external vbs-script

我目前正在研究如何更新 hta 文件中的 div 元素。 hta 调用一个 vbs 文件,其中实际脚本是 运行。我目前的意图是将 hta 启动时的时间写到 div 中,以替换已经存在的文本。最后,这将成为我公司部门的一个工具,我们在那里管理闯入。但现在我什至无法弄清楚如何将文本写入 div...

我试图通过 document.getElementByID 和 parent.dokument.getElementByID 获取元素,但它一直告诉我“需要对象”,我在其中写了“oRefreshed.innerHTML...”

有人可以帮助我吗?

编辑:作者姓名

文件(请注意,在结束 html 标签下方,vbs 文件开始)

<html>
    <head>
        <HTA:APPLICATION 
             id="oHTA"
             APPLICATIONNAME="PausentoolByFF"
             BORDER="thin"
             ICON=".\data\icon.ico"
             MAXIMIZEBUTTON="yes"
             SCROLL="auto"
             SELECTION="no"
             SINGLEINSTANCE="yes"
             SYSMENU="yes"
             VERSION="0.1.0"
        />
        <title>
        Pausentool
        </title>
        <link rel="stylesheet" href=".\data\style.css">
        <meta name="author" content="">
        <meta name="version" content=oHTA.VERSION>
        <script Language="vbscript">
            Dim loaded
            loaded = True
            window.resizeTo 1100,620
        </script>
        <script language="VBScript" src=".\data\pausentool.vbs"></script>
    </head>
    <body>
        <p>
            <table>
                <tr>
                    <div class="content menu" id="admin">
                        <input type="button" class="button" id="adminButton" accesskey="a" title="Admin-Screen öffnen (Alt + A)" value="Administrieren">
                    </div>
                    <div class="content menu" id="getKP">
                        <input type="button" class="button" id="getKPButton" accesskey="p" title="Pausenkarte ziehen (Alt + P)" value="Pausenkarte">
                    </div>
                    <div class="content menu" id="refreshBtn">
                        <input type="button" class="button" id="refreshButton" accesskey="r" title="Pausentool aktualisieren (Alt + R)" value="Aktualisieren" onClick="window.location.reload()">
                    </div>
                    <div class="SLA">
                        <div class="content">
                            <span id="SLA1">Anzeige der</span><br>
                            <span id="SLA2">SLA-DATEN</span>
                        </div>
                    </div>
                    <div class="logo" id="logo">
                        <img src="./data/logo.png">
                    </div>
                    <div class="refreshed">
                        zuletzt aktualisiert:<br>
                        <div id="refreshed">noch nie (tbd)</div>
                    </div>
                    </td>
                </tr>
            </table>
        </p>
        <p>
            <table>
                <tr>
                    <th class="content">
                        <input type="button" class="button" accesskey="1" title="diesen Pausenslot w&auml;hlen (Alt + 1)" value="11:30 - 12:00">
                    </th>
                    <th class="content">
                        <input type="button" class="button" accesskey="2" title="diesen Pausenslot w&auml;hlen (Alt + 2)" value="12:10 - 12:40">
                    </th>
                    <th class="content">
                        <input type="button" class="button" accesskey="3" title="diesen Pausenslot w&auml;hlen (Alt + 3)" value="12:50 - 13:20">
                    </th>
                    <th class="content">
                        <input type="button" class="button" accesskey="4" title="diesen Pausenslot w&auml;hlen (Alt + 4)" value="13:30 - 14:00">
                    </th>
                    <th class="content">
                        <input type="button" class="button" accesskey="5" title="diesen Pausenslot w&auml;hlen (Alt + 5)" value="14:10 - 14:40">
                    </th>
                    <th class="content">
                        keine Mittagspause
                    </th>
                </tr>
                <tr>
                    <td class="content">
                        <table id="1">
                            
                        </table>
                    </td>
                    <td class="content">
                        <table id="2">
                            
                        </table>
                    </td>
                    <td class="content">
                        <table id="3">
                            
                        </table>
                    </td>
                    <td class="content">
                        <table id="4">
                            
                        </table>
                    </td>
                    <td class="content">
                        <table id="5">
                            
                        </table>
                    </td>
                    <td class="content">
                        <table id="0">
                            
                        </table>
                    </td>
                </tr>
            </table>
        </p>
    </body>
</html>


If loaded Then
    'do stuff
    sHour = Hour(Now())
    sMinute = Minute(Now())
    sSec = Second(Now())
    If Len(sSec) = 1 Then sSec = "0" & sSec
    If Len(sMinute) = 1 Then sMinute = "0" & sMinute
    If Len(sHour) = 1 Then sHour = "0" & sHour
    Dim oRefreshed
    Set oRefreshed = parent.document.getElementById("refreshed")
    oRefreshed.innerHTML = sHour & ":" & sMinute & ":" & sSec
Else
    Dim Msg, Style, Title
    Msg = "Dieses Script kann nicht alleine gestartet werden. Bitte nutze stattdessen die Pausentool.hta! Bei Fragen wende dich gerne an den Entwickler."    ' Define message
    Style = vbOKOnly + vbCritical + vbDefaultButton2    ' Define buttons
    Title = "falsche Datei"    ' Define title
    MsgBox Msg, Style, Title 'show Messagebox
    
End If```

您遇到错误是因为 VBScript 代码在页面加载前 运行ning。该代码需要在“Sub window_OnLoad”下。另外,您可以直接引用“refreshed”。在这种情况下无需使用 GetElementByID。对 class 和 ID 使用相同的名称可以正常工作,但为了便于阅读,我会避免这样做。此外,HTA 应具有 DOCTYPE 和 X-UA-Compatible 声明,否则,它将在 IE 5 模式下变为 运行。您可能还需要对特殊字符使用 UTF-8。

下面是对您的 HTA 进行这些更改的快速编辑(我将 VBScript 代码直接放在 HTA 中,但如果它是外部的,它也可以正常工作)。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
  <HTA:APPLICATION 
  id="oHTA"
  APPLICATIONNAME="PausentoolByFF"
  BORDER="thin"
  ICON=".\data\icon.ico"
  MAXIMIZEBUTTON="yes"
  SCROLL="auto"
  SELECTION="no"
  SINGLEINSTANCE="yes"
  SYSMENU="yes"
  VERSION="0.1.0"
  />
<title>
  Pausentool
</title>
<meta name="author" content="">
<meta name="version" content=oHTA.VERSION>
<script Language="vbscript">
window.resizeTo 1100,620
Msg = "Dieses Script kann nicht alleine gestartet werden. Bitte nutze stattdessen die Pausentool.hta! Bei Fragen wende dich gerne an den Entwickler."    ' Define message
Style = vbOKOnly + vbCritical + vbDefaultButton2    ' Define buttons
Title = "falsche Datei"    ' Define title
MsgBox Msg, Style, Title 'show Messagebox

Sub window_OnLoad
  sHour = Hour(Now())
  sMinute = Minute(Now())
  sSec = Second(Now())
  If Len(sSec) = 1 Then sSec = "0" & sSec
  If Len(sMinute) = 1 Then sMinute = "0" & sMinute
  If Len(sHour) = 1 Then sHour = "0" & sHour
  refreshed.innerHTML = sHour & ":" & sMinute & ":" & sSec
End Sub

</script>
</head>
<body>
<p>
<table>
<tr>
<div class="content menu" id="admin">
    <input type="button" class="button" id="adminButton" accesskey="a" title="Admin-Screen öffnen (Alt + A)" value="Administrieren">
</div>
<div class="content menu" id="getKP">
    <input type="button" class="button" id="getKPButton" accesskey="p" title="Pausenkarte ziehen (Alt + P)" value="Pausenkarte">
</div>
<div class="content menu" id="refreshBtn">
    <input type="button" class="button" id="refreshButton" accesskey="r" title="Pausentool aktualisieren (Alt + R)" value="Aktualisieren" onClick="window.location.reload()">
</div>
<div class="SLA">
    <div class="content">
        <span id="SLA1">Anzeige der</span><br>
        <span id="SLA2">SLA-DATEN</span>
    </div>
</div>
<div class="logo" id="logo">
    <img src="./data/logo.png">
</div>
<div class="refreshed">
    zuletzt aktualisiert:<br>
    <div id="refreshed">noch nie (tbd)</div>
</div>
</td>
</tr>
</table>
</p>
<p>
<table>
<tr>
<th class="content">
    <input type="button" class="button" accesskey="1" title="diesen Pausenslot w&auml;hlen (Alt + 1)" value="11:30 - 12:00">
</th>
<th class="content">
    <input type="button" class="button" accesskey="2" title="diesen Pausenslot w&auml;hlen (Alt + 2)" value="12:10 - 12:40">
</th>
<th class="content">
    <input type="button" class="button" accesskey="3" title="diesen Pausenslot w&auml;hlen (Alt + 3)" value="12:50 - 13:20">
</th>
<th class="content">
    <input type="button" class="button" accesskey="4" title="diesen Pausenslot w&auml;hlen (Alt + 4)" value="13:30 - 14:00">
</th>
<th class="content">
    <input type="button" class="button" accesskey="5" title="diesen Pausenslot w&auml;hlen (Alt + 5)" value="14:10 - 14:40">
</th>
<th class="content">
    keine Mittagspause
</th>
</tr>
<tr>
<td class="content">
    <table id="1">
        
    </table>
</td>
<td class="content">
    <table id="2">
        
    </table>
</td>
<td class="content">
    <table id="3">
        
    </table>
</td>
<td class="content">
    <table id="4">
        
    </table>
</td>
<td class="content">
    <table id="5">
        
    </table>
</td>
<td class="content">
    <table id="0">
        
    </table>
</td>
</tr>
</table>
</p>
</body>
</html>