简体   繁体   English

将onclick事件添加到javascript中新创建的表格标题中,但是onclick函数被添加到页面上的所有内容中,而不仅仅是我想要的一个

[英]Adding onclick event to newly created table header in javascript, but the onclick function is added to all th's on the page, not just the one I want

I'm generating a table in js and using a function as follows for some of the headers: 我在js中生成一个表,并对某些标头使用如下功能:

function generateTableHeader(table) {
    var tr = document.createElement('tr');

    var th = document.createElement('th');

    th.onclick = function(){
        toggleHidden("hide_header_" + table_count);
    };

    th.appendChild(document.createTextNode(key));
    tr.appendChild(th);    


    table.appendChild(tr);
}

My toggle hidden function just hides all elements of the class "hide_header_{some number}". 我的切换隐藏功能仅隐藏“ hide_header_ {some number}”类的所有元素。 It works fine. 工作正常。

The problem is, I call this generateTableHeader function several times, creating headers for multiple tables - each time it's called table_count (global variable) is incremented and I expected this function to hide only the class of elements with the number it has when the onclick event is created. 问题是,我多次调用了generateTableHeader函数,为多个表创建了标题-每次调用table_count(全局变量)时都会递增,并且我希望此函数仅隐藏具有onclick事件时具有的元素数的元素类被建造。 Instead, whenever I click on any of the headers, they always hide the last element created. 相反,每当我单击任何标题时,它们始终会隐藏最后创建的元素。

The function is called 4 times to create 4 table headers in 4 different tables - each has a column of td's with the correct class name (ie "hide_header_1", "hide_header_2" and so on). 该函数被调用4次,以在4个不同的表中创建4个表头-每个表头都有一列带有正确类名的td(即“ hide_header_1”,“ hide_header_2”等)。 When I click on the first header, the fourth table column is hidden/show. 当我单击第一个标题时,第四个表列被隐藏/显示。 When I click the second, third, or fourth, still only the fourth column is hidden/show. 当我单击第二,第三或第四时,仍然仅隐藏/显示第四列。 I want each th click to hide only the td's in that table. 我希望每次单击都只隐藏该表中的td。

It's as if the th onclick event I am creating is being overwritten each time I call this function, not sure why. 好像我每次调用此函数时正在创建的onclick事件都被覆盖,不确定为什么。 Any ideas? 有任何想法吗?

Any help would be appreciated! 任何帮助,将不胜感激!

This is a closure issue, you are using the same variable in all your click handlers. 这是一个关闭问题,您在所有点击处理程序中都使用了相同的变量。 Instead you can use a local variable with the value of the global variable at the time the set the click event. 而是可以在设置click事件时将局部变量与全局变量的值一起使用。

function generateTableHeader(table) {
    var tr = document.createElement('tr');

    var th = document.createElement('th');

    var local_table_count = table_count;
    th.onclick = function(){
        toggleHidden("hide_header_" + local_table_count);
    };

    th.appendChild(document.createTextNode(key));
    tr.appendChild(th);    


    table.appendChild(tr);
}

I think the answer probably lies within your own description. 我认为答案可能在您自己的描述中。 table_count is a global variable. table_count是全局变量。 Every time you increment it the reference that all of your event listeners are using in the line: 每次将其递增时,该行中所有事件侦听器都在使用该引用:

th.onclick = function(){
    toggleHidden("hide_header_" + table_count);
};

are having the reference updated. 正在更新参考。

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

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