简体   繁体   English

我们可以使用构造的动态jQuery选择器吗?

[英]Can we use a constructed dynamic jQuery selector?

I want to contruct a jquery selector as a string and pass its value to the selector. 我想将一个jQuery选择器构造为字符串,并将其值传递给选择器。

$(document).on( 'keyup', function( e ) {
    if( e.keyCode == 9 ) {
        //console.log( e.target );

        console.log(e.target.id);

        var preTabIndex = document.getElementById(e.target.id).tabIndex;
        var nextTabIndex = preTabIndex + 1;

        console.log(preTabIndex);
        console.log(nextTabIndex);

        //console.log($('[tabindex=3]')[0].id);

        var selector = "[tabindex=" + nextTabIndex + "]";

        console.log(selector);
        console.log($(selector)[0].Id);

        //document.getElementById($("[tabindex=3]")[0].id).focus();
        document.getElementById($(selector)[0].id).focus();
    }
} );

Can this be done? 能做到吗? I couldn't find it on my initial googling. 我最初在谷歌搜索时找不到。

With this i am getting an undefined when i do 有了这个,我做的时候就变得不确定

console.log($(selector)[0].Id);

Yes, you can. 是的你可以。 Make sure you use the . 确保使用。 or # to denote the target. 或#表示目标。 For example, 例如,

In your HTML: 在您的HTML中:

<p id="header">hello</p>

In your JS: 在您的JS中:

var my_selector = "#header";
$(my_selector).html('wow');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM