如何设置“如果对象存在”健康)状况?

发布于 2024-10-06 16:29:21 字数 347 浏览 1 评论 0原文

有没有办法检查对象是否存在?我不断收到“需要对象”错误。我知道该对象不存在,如果是这种情况,我想绕过我的代码的一部分。我不知道我没有尝试过...

    var codeName = document.getElementById('testCode');
    //I have tried
    if(codeName != null)
    if(codeName.length != 0)
    if(typeOf codeName != 'undefined')
    if(!codeName)
    if(codeName.value != null)

有什么方法可以查看对象是否存在?

Is there some way to check if an object exists? I keep getting an "object required" error. I know the object does not exist and I would like to bypass a part of my code should that be the case. I don't know what I have not tried...

    var codeName = document.getElementById('testCode');
    //I have tried
    if(codeName != null)
    if(codeName.length != 0)
    if(typeOf codeName != 'undefined')
    if(!codeName)
    if(codeName.value != null)

Is there any way to see if an object exists?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

冰魂雪魄 2024-10-13 16:29:21

getElementById 调用之后,codeName 要么是 DOM 元素,要么是 null。您可以使用警报来查看哪个:

alert(codeName);

所以 if (codename != null) 应该有效。

错误是否在发生之前就发生了?我会尝试添加警报以在代码运行时查看值。或者在调试器中单步执行此代码。

After the getElementById call, codeName is either a DOM Element or null. You can use an alert to see which:

alert(codeName);

So if (codename != null) should work.

Does the error happen before it gets that far? I would try adding alerts to see the values as the code runs. Or step through this code in a debugger.

遥远的绿洲 2024-10-13 16:29:21

尝试:

var codeName = document.getElementById(code[i]) || null;
if (codeName) {/* action when codeName != null */}

如果您想确保 codeName 是一个对象:

if (codeName && codeName instanceof Object) {
  /* action when codeName != null and Object */
}

Try:

var codeName = document.getElementById(code[i]) || null;
if (codeName) {/* action when codeName != null */}

if you want to be sure codeName is an Object:

if (codeName && codeName instanceof Object) {
  /* action when codeName != null and Object */
}
我不会写诗 2024-10-13 16:29:21
var codeList = document.getElementById('codeList');
if(!codeList && !codeList.value && !codeList.value.length) return;
var code = codeList.value.split(","),
    itemCount = code.length;
if(!itemCount) return;
for (var i=0, i<itemCount; i++) {
    var codeName = document.getElementById(code[i]);
    if(!codename || !codename.length) continue;
    //do something here...
}

我在这里有一个工作示例: http://jsbin.com/uduxe4/15

var codeList = document.getElementById('codeList');
if(!codeList && !codeList.value && !codeList.value.length) return;
var code = codeList.value.split(","),
    itemCount = code.length;
if(!itemCount) return;
for (var i=0, i<itemCount; i++) {
    var codeName = document.getElementById(code[i]);
    if(!codename || !codename.length) continue;
    //do something here...
}

I got a working example here: http://jsbin.com/uduxe4/15

行雁书 2024-10-13 16:29:21

我不知道 document 是什么,但您可以尝试类似的方法

if(document.getElementById('code'+[i]) == null)
{
//...do Something
}

,在使用它之前测试它是否存在......

I don't know what document is, but you could try something like

if(document.getElementById('code'+[i]) == null)
{
//...do Something
}

so testing if it exists before you using it...

衣神在巴黎 2024-10-13 16:29:21

我没有做很多 JS 编码,但看起来你的 [i] 是问题所在。据我所知,[]用于访问数组的字段,而你没有数组。只需使用“代码”+ i

I don't do a whole lot of JS coding, but it seems like your [i] is the problem. As far as I know, [] is used for accessing a field of an array, and you don't have an array. Just use "code" + i

緦唸λ蓇 2024-10-13 16:29:21

在 ruby​​ 中,nil 相当于 false

因此尝试仅检查:

if codeName

In ruby nil is equivalent to false.

So try to check just:

if codeName

咽泪装欢 2024-10-13 16:29:21
<div id='code1'></div>

var itemCount = 10;
var len = 10;
len = itemCount;
for (var i=0;i<len; i++) {
    var codeName = document.getElementById('code'+ i);
    if(codeName == null)
       alert("Nope " + i);
    else
        alert("Yep " + i);
}
<div id='code1'></div>

var itemCount = 10;
var len = 10;
len = itemCount;
for (var i=0;i<len; i++) {
    var codeName = document.getElementById('code'+ i);
    if(codeName == null)
       alert("Nope " + i);
    else
        alert("Yep " + i);
}
对你而言 2024-10-13 16:29:21

try/catch 对你有用吗? 示例

function toDoStuff(elem) {
    codeName = document.getElementById(elem);
    if (!codeName) throw "Object isn't here yet!"
}


for (var i = 0; i < 5; i++) {
    try {
        toDoStuff('someElem');
    } catch (err) {
        if (err == "Object isn't here yet!") {
            alert("Object isn't ready yet leaving loop!");
            break;
        }
    }
}

Would a try/catch work for you? Example

function toDoStuff(elem) {
    codeName = document.getElementById(elem);
    if (!codeName) throw "Object isn't here yet!"
}


for (var i = 0; i < 5; i++) {
    try {
        toDoStuff('someElem');
    } catch (err) {
        if (err == "Object isn't here yet!") {
            alert("Object isn't ready yet leaving loop!");
            break;
        }
    }
}
醉梦枕江山 2024-10-13 16:29:21

也许尝试检查您的对象是否继承了您的函数所需的类型?像这样:

if(codeName is String)

Maybe try checking that your object inherits from the type your function needs? Like this:

if(codeName is String)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文