【问题标题】:why does getElementByID return Nothing instead of NULL为什么 getElementByID 返回 Nothing 而不是 NULL
【发布时间】:2014-11-04 22:15:46
【问题描述】:

在 VBA 中,如果我使用 getElementByID("id_name") 并且 id 不存在,则该函数不返回任何内容而不是 null。这让我不知道 DOM 是否尚未渲染元素,或者该元素是否真的不存在。似乎规范要求返回 NULL 并且 NULL 不等于 Nothing。所以我的问题是这个 DOM 函数是否返回 NULL、Nothing 或者它是否依赖于我缺少的东西?谢谢

片段

If Not IsNull(p_IE.Document.getElementById(MAIN_SR_CONTAINER)) Then
 ' If I do not receive NULL I want to assume that I can grab the element.
 ' Still, I verify that the element is not Nothing

 ' problem is that NULL <> Nothing so if the element does not exist my code loops for eternity
 ' I do look at the readystate of the p_IE object and wait till it = 4
 ' But the elements may be being created by embedded javascript on the fly

    Set elMainSRContainer = p_IE.Document.getElementById(MAIN_SR_CONTAINER)
    Do While elMainSRContainer Is Nothing
        Set elMainSRContainer = p_IE.Document.getElementById(MAIN_SR_CONTAINER)
    Loop

    :
    :
Else
    ' bail
End If

【问题讨论】:

    标签: vba internet-explorer null getelementbyid nothing


    【解决方案1】:

    MSDN documentation for getElementById 表示方法返回值是 IHTMLElement 类型。这将是 VBA 中的 Object 类型。文档继续说该方法

    返回具有指定 ID 的第一个对象,如果没有匹配则返回 null。

    我的猜测是,因为在 VBA 中,Objects 不能容纳 Null,所以 Null 被解释为 Nothing

    我会尝试改变

    If Not IsNull(p_IE.Document.getElementById(MAIN_SR_CONTAINER)) Then
    

    If Not (p_IE.Document.getElementById(MAIN_SR_CONTAINER) Is Nothing Then
    

    【讨论】:

      【解决方案2】:

      一切正常。您只需要将 ID 名称放在引号“”中

      所以

      If Not IsNull(p_IE.Document.getElementById("MAIN_SR_CONTAINER")) Then
      

      试试看。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多