[英]how to add class and elements (<p>) to appended <li> in jQuery/JS
Im fairly new with jQuery, JS & AJAX so please bear with me. 我是jQuery,JS和AJAX的新手,所以请多多包涵。
I tried to create a dynamic that will generate its content based on the result in my DB. 我试图创建一个动态数据库,该数据库将根据数据库中的结果生成其内容。 This is the HTML code i want to generate with jQuery/JS :
这是我想用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>
It a list item with a class and some HTML elements in it. 它是一个列表类,其中包含一个类和一些HTML元素。
So i tried something like this : 所以我尝试了这样的事情:
$.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++;
});
}
}
});
The AJAX & CSS seems dont read the class="box"
. AJAX和CSS似乎没有读取
class="box"
。
I have done some research and trials, and i can do something like $('#list').append('<li><a href="#header">Back to top</a></li>');
我已经进行了一些研究和试验,并且可以做类似
$('#list').append('<li><a href="#header">Back to top</a></li>');
easily. 容易。 But i dont know why my code is not working.
但我不知道为什么我的代码无法正常工作。
Note : I have tried to manually code the HTML above, and the AJAX for generating data is already working. 注意:我试图手动编码上面的HTML,并且用于生成数据的AJAX已在起作用。 So it seems the only problem is the append now.
因此,似乎唯一的问题是现在添加。
Any help is appreciated. 任何帮助表示赞赏。 Thanks :D
感谢:D
The .append() does not work as a string concatenate operation, you need to create a dom structure and pass it to the .append() call .append()不能作为字符串连接操作,您需要创建一个dom结构并将其传递给.append()调用
Try 尝试
$('#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>');
But the code should be as simple as 但是代码应该很简单
$.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>');
})
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.