从jquery对象获取元素
我有这个html元素:从jquery对象获取元素,jquery,html,Jquery,Html,我有这个html元素: <tr style="cursor: pointer" onclick="doWithThisElement(this)"> <td scope="row">position</td> <td>Machine 345</td> <td>30</td> </tr> 当我记录它时,它会给我: 机器345 但是作为一个对象。香草js解决
<tr style="cursor: pointer" onclick="doWithThisElement(this)">
<td scope="row">position</td>
<td>Machine 345</td>
<td>30</td>
</tr>
当我记录它时,它会给我:
机器345
但是作为一个对象。香草js解决方案
function doWithThisElement(elem) {
var test = elem.children[1].innerText;
console.log(test);
}
function doWithThisElement(elem) {
var test = $(elem).children().get(1).textContent
console.log( test );
}
Jquery解决方案
function doWithThisElement(elem) {
var test = elem.children[1].innerText;
console.log(test);
}
function doWithThisElement(elem) {
var test = $(elem).children().get(1).textContent
console.log( test );
}
我不知道你是否想要得到一个特定的列,或者如果你点击一个特定的行,你是否想要捕获任何数据。否则,我可以向您提出以下解决方案: JS HTML
位置
机器345
30
您在jquery对象上尝试过.text()吗?$(elem).children()[1]。get(0)
没有什么意义。dom节点不应该有get方法吗?我尝试了text(),但是yes get(0)没有意义:),我拼命地尝试着脑海中出现的一切,却忘了删除它
$( document ).ready(function() {
$("#your-id").on('click', function(){
console.log( $( this ).children().get(1).textContent );
});
});
<table>
<tr style="cursor: pointer" id="your-id">
<td scope="row">position</td>
<td>Machine 345</td>
<td>30</td>
</tr>
</table>