简体   繁体   English

JavaScript抛出TypeError表示我的变量未定义

[英]JavaScript throws TypeError saying that my variable is undefined

I don't have much experience in JavaScript, so far I have this: 我没有太多的JavaScript经验,到目前为止,我有这样的经验:

  function loop() {
    var authorDivs = document.getElementById('ctl00_MainContent_MCPObjectInfo_dvCreatorView').getElementsByTagName("div");

    for (var i = 0; i < authorDivs.length; i++) {
        var divOfDiv = authorDivs[i].getElementsByTagName("div");

        if (typeof divOfDiv.item(i) === 'undefined' || divOfDiv.item(i) === null) {
            console.log("This is undefined or null");
        }
        else {
            var realDivs = divOfDiv.item(i);
            realDivs.item(i).textContent = "please work plz";
        }
    }
}

I get the following error from the console in FireFox: TypeError: realDivs is undefined on this line: realDivs.item(i).innerHTML = "please work plz"; 我从FireFox的控制台中收到以下错误: TypeError: realDivs is undefined在此行上TypeError: realDivs is undefinedrealDivs.item(i).innerHTML = "please work plz";

Essentially what I have (in my mind) is a loop that goes through authorDivs and gets all of the divs within those divs and saves them in divOfDiv . 基本上我有什么(在我心中)是一个循环,通过去authorDivs ,并得到所有的divs那些内divs和保存他们divOfDiv I then check to see if the divs in divOfDiv are undefined or null , if they are not then those divs get saved in a variable realDivs which I then use to edit the innerHTML. 然后,我检查divOfDiv中的divs是否undefined或为null ,如果不是,则将这些divs保存在变量realDivs ,然后使用该变量来编辑innerHTML。 That's what I'd ideally like to see happen, what is causing the error? 这就是我理想中希望发生的情况,是什么导致错误? What am I doing wrong? 我究竟做错了什么?

Note: I do not have access to jQuery but only JavaScript. 注意:我无权访问jQuery,而只能访问JavaScript。

Edit: I've added the changes suggested below and its fixed that -- thanks! 编辑:我已经添加了下面建议的更改,并对其进行了修复-谢谢! But I'm now getting the following error: TypeError: realDivs.item is not a function 但是我现在收到以下错误: TypeError: realDivs.item is not a function

What is causing that? 是什么原因造成的? And on another note how do I know when I'm dealing with an array and when I'm dealing with an HTMLCollection? 在另一个注释上,我如何知道何时处理数组以及何时处理HTMLCollection? Do you just assume? 你只是假设吗? I've never used a loosely typed language before so its new to me. 我以前从未使用过松散类型的语言,因此它对我来说是新的。

it will happen in case when your if (typeof divOfDiv.item(i) === 'undefined' || 'null') returns true. 当您的if (typeof divOfDiv.item(i) === 'undefined' || 'null')返回true时,就会发生这种情况。 Then you never initialize realDivs (what would happen if condition was falsy). 然后,您将永远不会初始化realDivs (如果条件错误,会发生什么情况)。 Later you try to call item function on that unitialized object 稍后,您尝试在该统一对象上调用item函数

Well, you'll need to move that code inside the conditional block that is supposed to prevent it! 好吧,您需要将该代码移入应该阻止该条件的条件块中! Also, || "null" 另外, || "null" || "null" is not going to work as you expect, you'll need to check for || divOfDiv.item(i) === null || "null"不会按您预期的那样工作,您需要检查|| divOfDiv.item(i) === null || divOfDiv.item(i) === null explicitly. || divOfDiv.item(i) === null明确为|| divOfDiv.item(i) === null

So try 所以尝试

for (var i = 0; i < authorDivs.length; i++) {
    var divOfDiv = authorDivs[i].getElementsByTagName("div");

    if (divOfDiv.item(i) == null) {
        console.log("This is undefined or null");
    } else {
        var realDivs = divOfDiv.item(i)
        realDivs.item(i).innerHTML = "please work plz";
        console.log(divOfDiv.item(i));
    }
}

However, that still doesn't really work for two reasons: 但是,由于以下两个原因,它仍然不能真正起作用:

  • The i index you use to access the i -th divOfDiv comes from the iteration over authorDivs - hardly what you want. i的索引使用来访问idivOfDiv来自迭代过authorDivs -你想要几乎没什么。 Instead, use a second loop over all divOfDiv s. 而是在所有divOfDiv使用第二个循环。
  • Your realDivs variable does hold a single <div> , which does not have an .item method. 您的realDivs变量确实包含一个<div> ,该<div>没有.item方法。 You'd just directly access its .innerHTML property. 您只需直接访问其.innerHTML属性。

So you should use 所以你应该使用

var authorDivs = document.getElementById('authorView').getElementsByTagName("div");
for (var i=0; i<authorDivs.length; i++) {
    var divsOfDiv = authorDivs.item(i).getElementsByTagName("div");

    for (var j=0; j<divsOfDiv.length; j++) {
        var realDiv = divsOfDiv.item(j);
        realDiv.innerHTML = "please work plz";
        console.log(realDiv);
    }
}

There are two problems in the code. 代码中有两个问题。

  1. comparing DOM object with 'undefined' and null. 比较DOM对象是否为'undefined'和null。 If div tag is not available in authorDivs[i], it will return empty DOM array. 如果在authorDivs [i]中div标签不可用,它将返回空的DOM数组。 So, comparision of empty DOM array with undefined and null is not good approach. 因此,将空DOM数组与undefined和null进行比较不是一个好方法。 We can use array length property for doing validation. 我们可以使用数组长度属性进行验证。

    divOfDiv = authorDivs[i].getElementsByTagName("div"); divOfDiv = authorDivs [i] .getElementsByTagName(“ div”);
    if(divOfDiv.length > 0) { console statement} if(divOfDiv.length> 0){控制台语句}

  2. As item(i) is already return single DOM element, item(i) of "realDivs" variable is not proper approach. 由于item(i)已经返回单个DOM元素,因此“ realDivs”变量的item(i)是不合适的方法。 In addition to this, innerHTML method needs to be used after validating whether realDivs contains DOM element. 除此之外,在验证realDivs是否包含DOM元素之后,需要使用innerHTML方法。 Please update the code as below. 请如下更新代码。

    var realDivs = divOfDiv.item(i); var realDivs = divOfDiv.item(i); realDivs ? realDivs? (realDivs.innerHTML = "please work plz"): null; (realDivs.innerHTML =“ please work plz”):null;

Note : item(i) will return null if DOM is not available. 注意:如果DOM不可用,则item(i)将返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM