【问题标题】:Concatenate $(this) to string in jquery selector将 $(this) 连接到 jquery 选择器中的字符串
【发布时间】:2018-07-29 10:53:40
【问题描述】:

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

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

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

    });

上面的代码有什么问题?

【问题讨论】:

  • 向我们提供您的 HTML 标记(表格)
  • 你不能连接 this 这是一个带有字符串的对象。如果没有相关的 html 示例和更好的解释,不清楚您要完成什么。请看minimal reproducible example

标签: jquery jquery-selectors concatenation


【解决方案1】:

在 jQuery 中 this 引用 DOM 元素。

如果您想以 jQuery 方式检查 .td_inputstd 的子级)是否隐藏,那么您应该使用 .children() 方法:

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

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

});

【讨论】:

  • this 不是事件对象。它是一个dom元素
  • @charlietfl 对!要快速回答!会改变的。
【解决方案2】:

您要重用this 的一般格式是:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2011-01-13
    • 1970-01-01
    • 2018-07-14
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多