【问题标题】:jQuery selectors for list elements列表元素的 jQuery 选择器
【发布时间】:2012-06-02 01:24:41
【问题描述】:

我正在使用 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 和许多其他多重选择器,但无济于事。

有什么想法吗?

【问题讨论】:

    标签: jquery html html-lists getjson


    【解决方案1】:

    您何时尝试执行为列表项着色的命令?相信你一定要在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'});
    });
    

    【讨论】:

      猜你喜欢
      • 2019-05-21
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2010-12-04
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多