Jquery 如何访问下一个元素并检查它是否有特定的类
我需要根据以下第二个Jquery 如何访问下一个元素并检查它是否有特定的类,jquery,Jquery,我需要根据以下第二个td元素是否具有特定的类来调用函数 这是HTML <tr id="939"> <td data-id="939">Test2</td> <td class="hold" data-id="939"></td> </tr> <tr id="938"> <td data-id="938">Test1</td> <td class="someother
td
元素是否具有特定的类来调用函数
这是HTML
<tr id="939">
<td data-id="939">Test2</td>
<td class="hold" data-id="939"></td>
</tr>
<tr id="938">
<td data-id="938">Test1</td>
<td class="someotherclass" data-id="938"></td>
</tr>
我该怎么做?我尝试过使用next
和nearest
,但没有任何效果
编辑:
我做了一个修改:$(这个)
不是指你的td类,而是指你的getJSON
。试试这个:
$('body').on('click', '#article td:first-child', function(e){
$nextObj = $(this).next();
//YOUR getJSON function
if($nextObj.hasClass("hold")){
// Call my function
}
});
试试这个:
$('body').on('click', '#article td:first-child', function(e){
$nextObj = $(this).next();
//YOUR getJSON function
if($nextObj.hasClass("hold")){
// Call my function
}
});
$("#article td:first-child").click(function() {
if($(this).next().hasClass("hold")){
console.log("works");
}
});
小提琴:
hasClass(“.hold”)
应该是hasClass(“hold”)
(去掉点)。谢谢。我刚试过,但还是一无所获。我想我已经很接近了你应该发布一些提琴演示,我们在这里看不到整个HTML结构,也不清楚什么东西在这里不起作用。一旦您删除
。如果我们无法重现问题,我们将无法帮助您解决。再见,谢谢GGio。我试过了,但没用。控制台为$nextObj
显示的是jQuery[]
GGio,您能告诉我为什么我原来在getJSON函数中的东西不起作用吗?@NaN因为getJSON函数中的$(this)不是指click事件,而是指getJSON对象。Joe,这不起作用。这正是我已经拥有的。我没有看到你对JSON函数的评论。张贴的提琴只是简化了用例。:)是的,我得多用小提琴。我只是觉得这是一个快速的解决方案。