如何将类和元素(<p>)添加到附加的<李>在jQuery/JS中

how to add class and elements (<p>) to appended <li> in jQuery/JS

本文关键字:lt gt jQuery JS 添加 元素      更新时间:2023-09-26

我对jQuery、JS&AJAX,所以请耐心等待。

我试图创建一个动态,根据数据库中的结果生成其内容。这是我想用jQuery/JS:生成的HTML代码

<li class="box">
            <img class="picture" src="images/HotPromo/tagPhoto1.png"/>
            <p class="name"><b>Name</b></p>
            <p class="address">Address</p>
        </li>

它是一个列表项,其中包含一个类和一些HTML元素

所以我尝试了这样的东西:

$.ajax({
            url: "http://localhost/jwmws/index.php/jwm/search/msmall/"+keyword, //This is the current doc
            type: "GET",
            error : function(jq, st, err) {
                alert(st + " : " + err);
            },
            success: function(result){
                if(result.length == 0)
                {
                    //temp
                    alert("not found");
                }
                else{
                    for(var i = 0; i < result.length; i++)
                    {
                        //generate <li>
                        $('#list').append('<li class="box">');
                        $('#list').append('<img class="picture" src="images/HotPromo/tagPhoto1.png"/>');
                        $('#list').append('<p class="name"><b>Name</b></p>');
                        $('#list').append('<p class="address">Address</p></li>');
                    }
                    var i=0;
                 //THIS PART IS ALREADY WORKING
                    $(".box").each(function(){
                            var name, address, picture = "";
                        if(i < result.length)
                        {
                            alert("generated");

                            name = result[i].name;
                            address = result[i].address;
                            picture = result[i].boxpicture;
                        }
                        $(this).find(".name").html(name);
                        $(this).find(".address").html(address);
                        $(this).find(".picture").attr("src", picture);
                        i++;
                    });
                }
            }
            });

AJAX&CSS似乎没有读取class="box"

我已经做了一些研究和试验,我可以很容易地做$('#list').append('<li><a href="#header">Back to top</a></li>');这样的事情。但我不知道为什么我的代码不起作用。

注意:我已经尝试过手动编写上面的HTML,用于生成数据的AJAX已经在工作了。所以现在唯一的问题似乎是附加。

感谢您的帮助。感谢:D

.append()不能作为字符串连接操作,您需要创建一个dom结构并将其传递给.append)调用

尝试

$('#list').append('<li class="box"><img class="picture" src="images/HotPromo/tagPhoto1.png"/><p class="name"><b>Name</b></p><p class="address">Address</p></li>');

但代码应该像一样简单

$.ajax({
    url: "http://localhost/jwmws/index.php/jwm/search/msmall/"+keyword, //This is the current doc
    type: "GET",
    error : function(jq, st, err) {
        alert(st + " : " + err);
    },
    success: function(result){
        if (result.length == 0) {
            // temp
            alert("not found");
        } else {
            var $list = $('#list');
            $.each(result, function(idx, item) {
                $list.append('<li class="box box' + idx
                             + '"><img class="picture" src="'
                             + item.boxpicture
                             + '"/><p class="name">' + item.name
                             + '</p><p class="address">'
                             + item.address + '</p></li>');
            })
        }
    }
});