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 回答