引用嵌套JQuery中的原始html元素

Reference original html elements in nested JQuery

本文关键字:原始 html 元素 嵌套 JQuery 引用      更新时间:2023-09-26

这个简单的问题是我对JQuery理解中的一个缺陷。

$('input').focus(function() {
    $('.popup').click(function() {
        //How do I reference the original input that 
        //triggered the focus event here?             
    });
});

正如你从标题中可以看出的那样,我在寻找答案时遇到了问题。。。

您可以添加这样的引用:

$('input').focus(function() {
    var originalInput = $(this);
    $('.popup').click(function() {
        console.log(originalInput.val());          
    });
});
$('input').focus(function() {
    var orig = $(this);
    $('.popup').click(function() {
        //How do I reference the original input that 
        //triggered the focus event here?             
        console.log(orig);
    });
});