我使用表格来显示项目,单元格[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!");
}
谢谢
答案 0 :(得分:5)
您传递的this
代表点击的元素,而不是事件对象。
您需要做的就是使用参数从.cells
获取同级.parentNode
,然后使用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);
}
&#13;
<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
&#13;
如果您知道索引号始终为1
和2
,那么您可以将其缩短。
alert(cells[1].textContent + " " + cells[2].textContent);
答案 1 :(得分:0)
您可以将js功能更改为此类
<script type="text/javascript">
function ClickPic(e) {
var s = '';
$(e).siblings().each(function() {
s = s + ',' + $(this).text()
});
alert(s);
}