jquery - 检查 jQuery 变量是否引用特定 DOM 元素

标签 jquery

http://jsfiddle.net/vq062uhk/

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');
    }
});

Demo

关于jquery - 检查 jQuery 变量是否引用特定 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25357125/

相关文章:

javascript - 如果表单输入中存在任何错误/过滤的单词,则显示警报

javascript - AngularJS:访问AngularJS Controller 中的jquery变量

javascript - jQuery:将 <span></span> 添加到 <li> 内的文本

javascript - 将鼠标悬停在图标上时按钮文本模糊

javascript - jquery 选择器提取子元素并附加到另一个元素

javascript - 自动替换为 jquery 正则表达式

php - 使用 id 属性将数据传递给 php 是否有意义?

python - 使用 jQuery 从 Web 服务器获取数据

jquery - jquery $ 实际上返回什么?

javascript - 样式表可以获取类或id来动态改变它吗?