在td -访问下一个单元格中单击
Onclick in td - access next cells
我正在使用一个表来显示项目,单元格[0]上的onclick事件应该输出(警告)来自单元格[1]和单元格[2]的数据。
我不确定用哪种方法可以访问它们。
这是我到目前为止的代码http://jsfiddle.net/5uua7eyx/3/
也许有一种方法可以使用我的变量输入
HTML
<table id="items">
<tr>
<td onclick="ClickPic(this)">Picture0</td>
<td>Name0</td>
<td>Price0</td>
</tr>
<tr>
<td onclick="ClickPic(this)">Picture1</td>
<td>Name1</td>
<td>Price1</td>
</tr>
</table>
JS
function ClickPic(e) {
"use strict";
var input = e.target;
alert("Clicked!");
}
谢谢
传递的是this
,它代表被单击的元素,而不是事件对象。
您需要做的就是使用参数从.parentNode
中获得兄弟.cells
,然后使用elem.cellIndex
来计算下一个索引:
function ClickPic(elem) {
"use strict";
var cells = elem.parentNode.cells;
var currIdx = elem.cellIndex;
alert(cells[currIdx + 1].textContent + " " + cells[currIdx + 2].textContent);
}
<table id="items">
<tr>
<td onclick="ClickPic(this)">Picture0</td>
<td>Name0</td>
<td>Price0</td>
</tr>
<tr>
<td onclick="ClickPic(this)">Picture1</td>
<td>Name1</td>
<td>Price1</td>
</tr>
</table>
x
如果你知道索引号总是1
和2
,那么你可以缩短它。
alert(cells[1].textContent + " " + cells[2].textContent);
你可以这样修改js函数
<script type="text/javascript">
function ClickPic(e) {
var s = '';
$(e).siblings().each(function() {
s = s + ',' + $(this).text()
});
alert(s);
}
相关文章:
- 使用jquery在单击时在单元格中输入值
- 在jQuery中输出单击单元格的内容
- 如何在单击单元格中的链接值时动态更改表行背景色
- 如何避免在自由jqgrid中单击单元格进行水平滚动编辑
- 谷歌工作表中的按钮,当单击时,会更改特定单元格的背景
- 如何使用单击属性和工具提示动态创建表格单元格,该工具提示将超链接到页面
- 高图热图:单击按钮或复选框时选择所有单元格
- 单击时将表格单元格从范围更改为输入
- 将单击表格单元格重定向到其中的
- 单击并用CTRL+C复制值时高亮显示html表格单元格
- 单击onclick单元格时滚动到顶部
- 在动态表格单元格跨度中单击时更新一个权重值
- 完整日历 (v2.x) 从事件中获取日单元格单击
- 从html表格输入单元格点击
- 向下滚动一个“;滚动单元“;单击
- 防止行选择上的单元格单击ExtJS 4.1
- 回调单元格点击在ui-grid
- 谷歌电子表格如何检测单元格点击而不改变焦点
- Kendo UI调度器不允许可靠的单元格点击事件处理
- 如何填写一个单元格点击在画布上的网格在html5