将$(this)连接到jquery选择器中的字符串

时间:2018-07-29 10:53:40

标签: jquery jquery-selectors concatenation

我正在尝试将$(this)连接到jquery选择器中的字符串,如下所示,但我的代码似乎不起作用:

    // when td is clicked
    $("body").on("click", "td" ,function() {

        if ( $(this + ' > .td_inputs').is(':hidden') ) {
            // 
        } 

    });

上面的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

在jQuery this中对DOM元素的引用。

如果您想以jQuery的方式检查.td_inputs(是td的子级)是否隐藏,则应使用.children()方法:

// when td is clicked
$("body").on("click", "td" ,function() {

    if ( $(this).children('.td_inputs').is(':hidden') ) {
        // 
    } 

});

答案 1 :(得分:1)

您要重复使用this的常规格式是:

$("> .td_inputs", this)...

$(this).find("> .td_inputs")...

在OP中,当您使用>时,可以直接使用.children,而无需使用>,例如

$(this).children(".td_inputs")...