【问题标题】:Update content of a div in HTA with external vbs-script使用外部 vbs 脚本更新 HTA 中 div 的内容
【发布时间】:2021-11-09 23:51:59
【问题描述】:

我目前正在尝试弄清楚如何更新 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```

【问题讨论】:

    标签: html vbscript hta


    【解决方案1】:

    您收到错误消息,因为 VBScript 代码在页面加载之前正在运行。该代码需要在“Sub window_OnLoad”下。此外,您可以直接引用“刷新”。在这种情况下,无需使用 GetElementByID。对类和 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>
    

    【讨论】:

    • 是否可以将 Sub window_OnLoad 放入我正在使用的 IF 中,还是应该将 IF 放入 sub?编辑:在 if 中使用 Sub 进行了尝试,并且可以正常工作。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多