1

jQuery と jQuery UI を使用しています。

getJSON 関数を使用して、リスト要素を作成し、それらを OL 要素に追加します。コードは次のとおりです。

$.getJSON("http://localhost/b/t.php", function(data){

        $.each(data, function(i, thet){
            //alert(thet.fname)
            var t = '<li class="name" id="p' + thet.pid + '">' + thet.pid + ' ' + thet.fname + ' ' + thet.lname + '</li>';

            $(t).appendTo("#" + thet.tour + 'list');

        });
    });

jQueryを使用してリスト要素を選択しようとしています。リストを HTML ページに手動で配置すると、機能します。ただし、プログラムでリスト項目を OL に追加しても、少なくとも私が試したことから選択することはできません。

$('li:last-child').css({color: 'red', backgroundColor: 'black'});

ID や他の多くの複数のセレクターを使用してみましたが、役に立ちませんでした。

何か案は?

4

1 に答える 1

2

リスト項目に色を付けるコマンドをいつ実行しようとしていますか? 次のように getJSON のコールバック関数の最後に配置する必要があると思います。

$.getJSON("http://localhost/b/t.php", function(data){
    $.each(data, function(i, thet){
        //alert(thet.fname)
        var t = '<li class="name" id="p' + thet.pid + '">' + thet.pid + ' ' + thet.fname + ' ' + thet.lname + '</li>';

        $(t).appendTo("#" + thet.tour + 'list');
    });
    $('li:last-child').css({color:'red',backgroundColor:'black'});
});
于 2009-02-22T03:40:31.867 に答える