var $currentRow = null;
$(function() {
$('.set').click(function() {
$currentRow = $(this).closest('tr');
});
$('.check').click(function() {
console.dir($(this).closest('tr'));
if ($currentRow == $(this).closest('tr')) {
alert('Yes');
}
else {
alert('No');
}
});
});
我有一个表和一个名为 $currentRow 的变量,它引用当前的 tr 元素。我希望能够检查某个项目(在本例中为“Is Current?”按钮)是否位于当前行中。
我可以给出 tr 的唯一 ID 或属性,但我想知道是否可以避免这种情况。
最佳答案
尝试以下代码,
$('.check').click(function(){
console.dir($(this).closest('tr'));
if ($currentRow && $currentRow.is($(this).closest('tr'))) {
alert('Yes');
}
else {
alert('No');
}
});
关于jquery - 检查 jQuery 变量是否引用特定 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25357125/