我有这个html元素:
<tr style="cursor: pointer" onclick="doWithThisElement(this)">
<td scope="row">position</td>
<td>Machine 345</td>
<td>30</td>
</tr>
onclick函数传递给我一个jquery对象。我想把'Machine 345'作为一个字符串。
到目前为止我尝试过的是:function doWithThisElement(elem) {
var test = $(elem).children()[1].get(0);
}
当我记录它时,它给了我:
机器345
但作为对象。
答案 0 :(得分:1)
Vanilla js解决方案......
function doWithThisElement(elem) {
var test = elem.children[1].innerText;
console.log(test);
}
Jquery解决方案......
function doWithThisElement(elem) {
var test = $(elem).children().get(1).textContent
console.log( test );
}
答案 1 :(得分:0)
function doWithThisElement(elem) {
var td = $(elem).children('td')[1];
var test = $(td).text();
}
答案 2 :(得分:0)
我不知道您是否想要获取特定列,或者如果您想要捕获任何数据,如果您单击特定行。否则,我可以建议你这个解决方案: https://jsfiddle.net/ckr41w86/
JS
$( document ).ready(function() {
$("#your-id").on('click', function(){
console.log( $( this ).children().get(1).textContent );
});
});
HTML
<table>
<tr style="cursor: pointer" id="your-id">
<td scope="row">position</td>
<td>Machine 345</td>
<td>30</td>
</tr>
</table>