0

我正在创建一个待办事项列表网络应用程序,它将每个待办事项设置为一个 cookie。我正在尝试遍历 jQuery.cookie 值以将我的 cookie 分成“待办事项”和“完成”。

我正在使用此代码,该代码目前获取所有 cookie,如果密钥是数字,那么它将将项目附加到我的tbody

var obj = $.cookie();

// add each one to the list!
$.each( obj, function( key, value ){

    if ( $.isNumeric(key) ) {

        // display the table
        $('.grid-100').animate({
            opacity: 1
        }, 300);

        // hide the intro page
        $('.introduction').hide();

        if( value.indexOf('class="done green"') ) {
            // append to tfoot                  
            $('tfoot').append(value);
        } else {
            // append to tbody                  
            $('tbody').append(value);
        }


    } else {

        // do nothing.

    }

});

然而这不起作用,我所有的待办事项都会被附加到,tfoot即使它们没有indexOf class="done green.

4

1 回答 1

3

indexOf returns -1 if the item is not found, which is a truthy value, so you need to check whether the index is greater than -1 to test whether the item is found in the string.

if( value.indexOf('class="done green"') >= 0 ) {
于 2013-10-29T16:22:11.850 回答