Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从单元格内的定位点访问表行对象?_Javascript_Jquery - Fatal编程技术网

Javascript 如何从单元格内的定位点访问表行对象?

Javascript 如何从单元格内的定位点访问表行对象?,javascript,jquery,Javascript,Jquery,我有一张几排的桌子。我想访问与触发函数populateRow()的锚点位于同一行中的值的单元格; (我希望我能解释清楚)。 你知道怎么做吗 <tr> <td> some value </td> <td> <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-b

我有一张几排的桌子。我想访问与触发函数populateRow()的锚点位于同一行中的值的单元格; (我希望我能解释清楚)。 你知道怎么做吗

<tr>
   <td>
         some value
   </td>
   <td>
       <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left" onclick="populateRow();">update</a>
   </td>
</tr>
<tr>
    <td>
          another value
    </td>
    <td>
        <a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline ui-corner-all ui-icon-check ui-btn-icon-left" onclick="populateRow();">update</a>
   </td>
</tr>

一些价值
另一个价值
函数需要知道单击了哪个锚点,因此您需要将
传递给它,给它一个对所述锚点的引用:

onclick="populateRow(this);"

function populateRow(anchor) {
  var row = $(anchor).closest("tr");
  var firstCellValue = row.find("td").first().text();
}
…然后使用
.closest()
在DOM中向上导航到包含的tr元素

或者,更好的方法是使用jQuery绑定click事件处理程序,而不是将其内联到每一行:

$(文档).ready(函数(){
$(“#idOfYourTable”)。在(“单击”,“a”,函数(e){
var行=$(此).tr;
var firstCellValue=row.find(“td”).first().text();
console.log(firstCellValue);
});
});

值1
价值2

我不知道
populateRow()
函数的作用是什么,但是如果您想获得相同
tr
的第一个
td
的值,那么您可以尝试测试:

onclick=“警报($(this).closest('tr').find('td:first').text())”

如果要在
populateRow()
函数中传递它,可以:

onclick=“populateRow($(this).closest('tr').find('td:first').text())”

或者。。。 如果您传递元素,然后在函数中处理它,则会更整洁:

onclick=“populateRow(此)”


希望这有帮助。祝你好运

假设我有五个带值的单元格,而不是锚点,我如何访问第五个单元格?或者
行.find(“td”).eq(4).text()
(注意
.eq()
方法的从零开始的索引),或者
行.find(“td:nth child(5)”.text()
(注意
:nth-child()
选择器的基于一的索引)。
function populateRow(elm) {
    var val = $(elm).closest('tr').find('td:first').text();
    alert(val);
}