document.getElementbyId:需要对象 [英] document.getElementbyId: Object required

查看:30
本文介绍了document.getElementbyId:需要对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在经典 ASP 页面上有一些代码,它试图获取

元素的 innerHTML.

 Dim oElm设置 oElm = document.getElementById("screenDiv")如果 oElm Is Nothing 那么'''别的document.getElementById("screenDiv").innerHTML = ""万一

我已经尝试了上述方法,但出现了 Object Required 错误.

我需要做什么来解决这个错误?

解决方案

注意:客户端 VBScript 已过时、已弃用,并且无法在任何现代浏览器(包括较新版本的 IE)中使用.

这意味着代码位于元素之前,所以当它执行时,比如元素不存在.

为了解决这个问题,让代码在 onload 事件中执行:

正确方法的信用去Korikulum和官方文档可以在此处找到.

实时测试用例.(仅限浏览器)

I have some code on a classic ASP page that is attempting to get the innerHTML of a <div> element.

    Dim oElm 
    Set oElm = document.getElementById("screenDiv")

    if oElm Is Nothing then
        '''
    else
        document.getElementById("screenDiv").innerHTML = ""
    end if

I've tried the above but I'm getting the Object Required error.

What do I need to do to get around this error?

解决方案

NOTE: client side VBScript is outdated, deprecated, and doesn't work in any modern browser including newer versions of IE.

This means the code is located before the element, so when it executes, such element does not exist.

To solve this, have the code execute in the onload event:

<script type="text/vbscript">
Set window.onload = GetRef("WindowLoad")
Function WindowLoad
    Dim oElm 
    Set oElm = document.getElementById("screenDiv")
    if oElm Is Nothing then
        MsgBox("element does not exist")
    else
        oElm.innerHTML = ""
    end if
End Function
</script>

Credit for the correct way goes to Korikulum, and the official documentation can be found here.

Live test case. (IE only)

这篇关于document.getElementbyId:需要对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆