【发布时间】:2013-01-14 01:46:29
【问题描述】:
我正在构建调度软件。我正在使用许多表格来显示时间/日期等。
在一个特定的表格中,有一些带有 ID 的单元格列出了日期和员工 ID 号。
我正在尝试设置交易班次系统。单击单元格时,将执行 ajax 函数。
这是一个示例单元格:
<td id="tblcell-2013_03_13-id_3" class="displayShift">$data placed here</td>
jQuery 代码:
$(document).ready(function () {
// I can't do this because each id is different
$("td#tblcell").click(function () {
alert('it works!');
});
// I can't do this because there other other td elements on the same page in other tables
$("td").click(function () {
alert('it works!');
});
});
如何设置一个在单击特定单元格时激活的函数,该函数还将 var 数据传递给脚本中的函数,该脚本具有无法在此函数中引用的其他 td 表元素?
谢谢
【问题讨论】:
-
请发布更多您的 HTML 以及单击单元格时应从其他单元格访问的其他内容的示例。最明显的是使用类选择器。
$('.displayShift').click(function() {console.log($(this));});其中$(this)是被点击的单元格。 -
我不明白,抱歉。哪些是
other td table elements并且所有内容都可以从任何地方引用,除非它是作用域的,但 DOM 元素不是这种情况,因此它们是可引用的。 -
kidwon,感谢您的回复。还有其他表 td 元素和其他具有自己 td 的表。因此,使用通用“td”选择器作为点击函数将触发所有这些其他 td 元素。我在这里设置了一个问题的演示:jsfiddle.net/NMGNb