简体   繁体   English

从动态生成的表中获取行和单元格的值

[英]Get row and cell value from dynamically generated table

I am trying to get the row and cell from a dynamically generated table onClick function. 我正在尝试从动态生成的表onClick函数获取行和单元格。 I am getting undefined currently. 我现在不确定。

xmlTitles = xmlDoc.getElementsByTagName("title");
                document.getElementById('playlist').innerHTML = "Playlist (" + xmlTitles.length + " videos)";
                document.getElementById('table').cellSpacing = "5px";
                for (var i = 0; i < xmlTitles.length; i++)
                {
                    tr = document.createElement('tr');
                    tr.style.height = "100px";
                    titles[i] = xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue;
                    image[i] = xmlDoc.getElementsByTagName("image")[i].childNodes[0].nodeValue;
                    filename[i] = xmlDoc.getElementsByTagName("filename")[i].childNodes[0].nodeValue;
                    td2 = document.createElement('td');
                    var _image = iPath + image[i];
                    td2.innerHTML = "<a href='javascript:callVideo(this);'><img src='" + _image + "'/></a>";
                    td2.style.verticalAlign = "top";
                    tr.appendChild(td2);
                    td1 = document.createElement('td');
                    var _title = titles[i];
                    td1.appendChild(document.createTextNode(_title));
                    td1.style.verticalAlign = "top";
                    td1.style.width = "200px";
                    td1.style.height = "100px";
                    td1.style.color = "#0000ff";
                    td1.style.fontFamily = "arial";
                    tr.appendChild(td1);
                    document.getElementById('table').appendChild(tr);
                }
            }

This is the function called: 该函数称为:

function callVideo(x)
            {
                console.log("Cell index: " + x.cellIndex);
}

Thanks. 谢谢。

I think the issue you are having is the context of your callVideo function. 我认为您遇到的问题是callVideo函数的上下文。 When you build up the dynamic table, you create an anchor tag inside td2 and then put the event handler on the a tags href, passing in this. 建立动态表时,可以在td2内创建一个定位标记,然后将事件处理程序放在a标记href中,并传入该标记。 I would suspect, that x in callVideo is not the TD element at all, but the anchor tag (a tag) and since a tags have no cellIndex, it will be undefined. 我会怀疑,callVideo中的x根本不是TD元素,而是锚标记(一个标记),并且由于标记没有cellIndex,因此它将是不确定的。 Add a console.log(x) into callVideo and establish exactly what x is. 将console.log(x)添加到callVideo中,并确切地确定x是什么。 If I am correct, the x.parentNode should get you the TD tag 如果我是正确的,则x.parentNode应该会为您提供TD标签

function callVideo(x)
{
    console.log(x); //Establish what x is
    console.log("Cell index: " + x.parentNode.cellIndex);
}

EDIT In response to comment. 编辑回应评论。

OK so you're having issues establishing the context of the eventHandler. 好的,因此在建立eventHandler的上下文时遇到问题。 I would change the way you are setting it up, as it not really best practice. 我会更改您的设置方式,因为这并不是最佳做法。 An <a> is an anchor/link, and running a function on click is changing its default behaviour, which means you have it add things like javascript(void) or href="#" and return false or e.preventDefault() just to stop it from doing what is suppose to do. <a>是锚点/链接,单击时运行函数会更改其默认行为,这意味着您需要添加诸如javascript(void)或href =“#”之类的内容,然后返回false或e.preventDefault()阻止它执行应做的事情。 All not best practice. 都不是最佳实践。 Why not just add a click handler to the img, or to the whole cell. 为什么不只是将单击处理程序添加到img或整个单元格。 eg (I removed the style stuff for clarity, I think that should be in CSS not Javascript too by the way) 例如(为了清晰起见,我删除了样式的东西,顺便说一句,我认为也应该使用CSS而不是Javascript)

xmlTitles = xmlDoc.getElementsByTagName("title");
document.getElementById('playlist').innerHTML = "Playlist (" + xmlTitles.length + " videos)";
document.getElementById('table').cellSpacing = "5px";
for (var i = 0; i < xmlTitles.length; i++)
{
    tr = document.createElement('tr');  
    titles[i] = xmlDoc.getElementsByTagName("title")[i].childNodes[0].nodeValue;
    image[i] = xmlDoc.getElementsByTagName("image")[i].childNodes[0].nodeValue;
    filename[i] = xmlDoc.getElementsByTagName("filename")[i].childNodes[0].nodeValue;
    td2 = document.createElement('td');
    //change here
    var imgEle = document.createElement('img')
    imgEle.src = iPath + image[i];
    imgEle.addEventListener('click', callVideo);
    td2.appendChild(imgEle);

    tr.appendChild(td2);
    td1 = document.createElement('td');
    var _title = titles[i];
    td1.appendChild(document.createTextNode(_title));
    tr.appendChild(td1);
    document.getElementById('table').appendChild(tr);
}

function callVideo()
{
    //'this' will be the img itself
    //so this.parentNode will be the cell (TD)
    console.log(this.parentNode.cellIndex);
    //this.parentNode.parentNode will be the row (TR)
    console.log(this.parentNode.parentNode.rowIndex);
}

OH and by the way, all the cells will return 0 as that is their index, remember Javascript indexes start from 0 (ie 0 is the first one, 1 is the second one etc). 顺便说一下,顺便说一下,所有单元格都将返回0,因为这是它们的索引,请记住Javascript索引从0开始(即0是第一个索引,1是第二个索引,依此类推)。 So to be honest, I'm not sure why you need the cellIndex, as it will always be 0 (ie the first cell) as you already know its in the first cell, because your code created it. 坦白说,我不确定为什么需要cellIndex,因为在第一个单元格中就已经知道它始终为0(即第一个单元格),因为您的代码创建了它。 Although if this is just a snippet from larger code, and you have images in other cells then it would make sense. 尽管如果这只是较大代码中的一小段,并且您在其他单元格中有图像,那还是有意义的。

two changes need to make, given your dynamic generation of DOM is correct. 鉴于您动态生成的DOM是正确的,因此需要进行两个更改。

  1. change <a href="javascript:callVideo(this)">...</a> to <a href="javascript:void(0);" onclick="callVideo(this)">...</a> <a href="javascript:callVideo(this)">...</a>更改为<a href="javascript:void(0);" onclick="callVideo(this)">...</a> <a href="javascript:void(0);" onclick="callVideo(this)">...</a> , to make it an event handler . <a href="javascript:void(0);" onclick="callVideo(this)">...</a> ,使其成为事件处理程序 Otherwise because the execution of the function via href is in global context, this will be effectively the global object which is window . 否则,因为通过href执行功能是在全局上下文中进行的, 因此这实际上是window全局对象。 Therefore your function will receive window as its parameter which is wrong 因此,您的函数将接收window作为其参数,这是错误的

  2. change the body of your callVideo function to OJay's solution callVideo函数的主体更改为OJay的解决方案

    \nfunction callVideo(x) 函数callVideo(x)\n{ {\n   console.log(x); console.log(x); //Establish what x is //确定x是什么\n   console.log("Cell index: " + x.parentNode.cellIndex); console.log(“单元格索引:” + x.parentNode.cellIndex);\n} } 

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

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