Javascript DOM 技巧
我在弄清楚如何在 Javascript 中自引用表行时遇到了一些麻烦。
这是简化的代码:
$( "#listitems tbody" ).append( "<tr onclick=\"editListItem(this)\">" +
"<td>" + id.val() + "</td>" +
"<td>" + title.val() + "</td>" +
"<td>" + description.val() + "</td>" +
"<td>" + TF + "</td>" +
"<td style=\"visibility: hidden;\">" + id.val() + "</td>" +
"</tr>" );
如您所见,我正在动态设置此表行和单元格的内容。但是,我需要将此表行的引用传递到其 onClick 函数中,该函数调用此 Javascript 方法:
function editListItem(obj) {
var id = obj.cells[4].innerHTML;
var cells = document.getElementById('listitems').rows[id].cells;
DATA[0] = cells[0].innerHTML;
DATA[1] = cells[1].innerHTML;
DATA[2] = cells[2].innerHTML;
DATA[3] = cells[3].innerHTML;
}
在该方法中,我需要访问包含在第四个“隐藏”单元格内的值。单击的表行。我通常只是将 ID 变量传递到 onClick 方法中,但该表的内容可以排序和重新排列,因此 ID 变量不一定对应于行的内容。
我在过去的四个小时里一直在谷歌搜索,但找不到这种情况的任何具体示例;我尝试过的所有操作都会触发一个 Javascript 错误,声明 obj.cells、obj[4]、obj.childNodes 等不存在,具体取决于我正在尝试哪一个。
有谁知道如何通过将“this”传递到表行的 onClick 来访问表行元素内的表单元格元素的innerHTML?
如果其中任何部分令人困惑,请告诉我,我正努力在今天离开之前完成这件事,否则我知道我会忘记这一切并必须重新开始。
I'm having a bit of trouble figuring out how to go about self-referencing a table row in Javascript.
Here's the boiled down code:
$( "#listitems tbody" ).append( "<tr onclick=\"editListItem(this)\">" +
"<td>" + id.val() + "</td>" +
"<td>" + title.val() + "</td>" +
"<td>" + description.val() + "</td>" +
"<td>" + TF + "</td>" +
"<td style=\"visibility: hidden;\">" + id.val() + "</td>" +
"</tr>" );
As you can see, I'm setting the contents of this table row and cells dynamically. However, I need to pass a reference to this table row into its onClick function, which calls this Javascript method:
function editListItem(obj) {
var id = obj.cells[4].innerHTML;
var cells = document.getElementById('listitems').rows[id].cells;
DATA[0] = cells[0].innerHTML;
DATA[1] = cells[1].innerHTML;
DATA[2] = cells[2].innerHTML;
DATA[3] = cells[3].innerHTML;
}
In this method, I need to access the value contained inside the 4th "hidden" cell of the table row that was clicked. I normally would just pass the ID variable into the onClick method, but this table's contents can be sorted and rearranged, so the ID variable will not necessarily correspond to the contents of the row.
I've been Googling for the past four hours but can't find any specific examples for this situation; everything I've tried just triggers a Javascript error proclaiming that obj.cells, obj[4], obj.childNodes, etc, does not exist, depending on which one I'm trying.
Does anyone know how you can access the innerHTML of table cell elements inside a table row element by passing "this" into the table row's onClick?
Please let me know if any part of this was confusing, I'm trying to get this done before I leave today or I know I'll forget it all and have to start all over.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
如果这不是您的意思,请告诉我。我不是 100% 确定我明白你的要求:)
how about:
And if that's not what you meant, let me know. I'm not 100% sure I understand what you're asking for :)
从代码的外观来看,您使用 jQuery,因此您可以通过使用轻松获取第 5 个 td
,然后使用该 jQuery 元素
签出 jQuery eq 选择器 了解更多信息
From the looks of your code, your using jQuery so you can easily grab the 5th td by using
and then do whatever you want with that jQuery element
checkout jQuery eq selector for more info
首先创建您自己的
元素,并使用适当的 JS 注册处理程序,而不是将处理程序嵌入到元素的属性中:
在
editListItem
中,这
将自动应用于整行元素:Create your
<tr>
element on its own first, and use proper JS to register the handler rather than embedding the handler in the element's attributes:In
editListItem
,this
will automatically apply to the whole row element: