简体   繁体   English

为什么这不将数据分配给在函数外部声明的变量

[英]Why is this not assigning data to a variable declared outside the function

Been messing around trying to get this to work, as you can see in the code below when the table row is selected then it outputs an alert of which row they selected and also assigns the row's id to a variable declared outside of the method. 正如您在下面的代码中看到的那样,当选中表行时,您一直在搞怪,以使其工作,然后输出警告,告知他们选择了哪一行,并将该行的ID分配给在方法外部声明的变量。 I'm using this variable to be able to manipulate the data. 我正在使用此变量来处理数据。

However, when it reaches another function the variable is undefined. 但是,当它到达另一个函数时,该变量未定义。 "Uncaught TypeError: Cannot read property 'parentElement' of undefined". “未捕获的TypeError:无法读取未定义的属性'parentElement'”。 Where and how should this variable be declared in order for me to be able to manipulate the data which is generated by the tables? 为了使我能够操作由表生成的数据,应在何处以及如何声明此变量?

var selectedItem;

function deleteRow() {
        selectedItem.parentElement.removeChild(selectedItem); 
}

Code snippet from inside function where the rows are created. 来自创建行的内部函数的代码片段。 See "Makes rows selectable: 请参阅“使行可选:

            var tr = document.getElementById("c_row" + r);
            for (var c = 0; c < columns; c++) 
            {
                var td = document.createElement("td");        
                td.innerText = cellText[c];
                tr.appendChild(td);

                //Makes the rows selectable
                tr.onclick = function(event) {
                    alert('Clicked: ' + event.target.parentNode.id)
                    selectedItem = event.target.parentNode.id;
                }                                     
            }

You need a js element ... selectedItem is a string 您需要一个js元素... selectedItem是一个字符串

function deleteRow() {
        document.getElementById(selectedItem).parentElement.removeChild(selectedItem); 
}

.id is a string, so it doesn't have a .parentNode property. .id是一个字符串,因此没有.parentNode属性。 Instead assign the element directly. 而是直接分配元素。

 selectedItem = event.target.parentNode;

And if it's the row itself that you're trying to assign, then just use this instead. 如果您要分配的是行本身,则只需使用this

selectedItem = this;

And if the variable is undefined, then it is likely that there has not yet been a click. 并且,如果变量未定义,则很可能尚未点击。 So you should check before trying to remove. 因此,您应在尝试删除之前进行检查。

function deleteRow() {
  if (selectedItem) {
    selectedItem.parentElement.removeChild(selectedItem); 
  }
}

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

相关问题 为什么函数无法访问外部声明的对象中的局部变量 - Why can't function access local variable in object declared outside 为什么不能将函数内部的值赋给外部声明的变量(globaly)? - Why cannot assign value inside a function to variable declared outside (globaly)? 在 JavaScript 中分配 function 之外的变量 - Assigning a variable outside of a function in JavaScript 函数外声明变量,现在不能使用 - Declared variable outside function, cant use it now 为什么在函数外部声明的变量为null? - why are variables declared outside a function null? 为什么变量在函数外声明时不起作用? - Why variables are not working when declared outside the function? 为什么将外部函数中定义的对象重新分配给与外部声明相同的变量名,这会产生错误? - Why reassigning an object defined in the outer function to a same variable name as declared outside is giving error? 为什么在函数外声明变量并在循环中赋值,使用的内存多于局部变量? - Why declaring a variable outside a function and assigning a value in a loop uses more memory than a local var? 使用在函数外部声明的变量的Javascript会返回值+未定义 - Javascript using a Variable declared outside the function returns the value + undefined along with it 在 function 之外声明的全局变量不起作用 - Global variable declared outside function doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM