开发者

jQuery startswith selector

function test() {
    var foo = [];
    $('#tree table').each(function(i, table) {
        foo[i] = $(table).text().trim();
    });

    var ch = 'G';

    for (j = 0; j <开发者_Python百科;= 12; j++) {
        if (foo[j] ^= ch) {
            alert(foo[j]);
        }
    }
}

[foo[j] ^= ch] startsWith selector isn't working in the above code. Help needed. Couldn't find any answer. Thanks in adv.


The starts-with selector is for use within jQuery selectors when selecting DOM elements based on the value of an attribute.

This does not work within regular javascript if() statements.

Try this instead:

var ch = 'G';
if ( foo[j].indexOf(ch) === 0 ) {
    alert( foo[j] );
}

This will check foo[j] to see if the index position (if any) of the value of the ch variable is position 0 (in other words, at the beginning).


EDIT:

Another alternative would be to specifically test the first character against ch. But this will only work for testing one character. If ch contains more than one, it will fail.

var ch = 'G';
if ( foo[j].charAt( 0 ) === ch ) {
    alert( foo[j] );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜