我正在从对web服务的ajax调用中生成一个列表视图。
这是代码:
var ajax = {
parseJSONP:function(result){
movieInfo.result = result.results;
$.each(result.results, function(i, row) {
console.log("Title" + row.title);
$('#movie-list').append('<li><a href="" data-id="' + row.id + '">`<img src="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185'+row.poster_path+'"/>`<h3>' + row.title + '</h3><p>' + row.vote_average + '/10</p></a></li>');
});
$('#movie-list').listview('refresh');
}};
我需要的是使<img src="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185'+row.poster_path+'"/>填充行元素的背景,并将行高提高30%。问题是我想不出用列表视图创建css的方法。
有办法这样做吗?我是javascript和jQuery Mobile1.4.3的新手。
诚挚的问候
发布于 2014-08-06 16:40:05
我注意到几件事:
考虑到这些因素,设置背景图像样式属性应该会很好。您还可能需要使用背景-重复和背景大小,以获得预期的效果。
我想出的是:
var ajax = {
parseJSONP:function(response){
var results = response.results;
movieInfo.result = results;
var $movieList = $("#movie-list");
for(var i = 0; i < results.length; i++) {
var row = results[i];
console.log("Title: " + row.title);
var listItem = document.createElement("li");
var anchor = document.createElement("a");
var header = document.createElement("h3");
var paragraph = document.createElement("p");
anchor.setAttribute("href", "");
anchor.setAttribute("data-id", row.id);
header.textContent = row.title;
paragraph.textContent = row.vote_average + "/10";
var backgroundImageUrl = "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185" + row.poster_path;
listItem.style.backgroundImage = "url('" + backgroundImageUrl + "')";
listItem.style.backgroundRepeat = "no-repeat";
listItem.style.backgroundSize = "cover";
anchor.appendChild(header);
anchor.appendChild(paragraph);
listItem.appendChild(anchor);
$movieList.append(listItem);
}
$movieList.listview('refresh');
//increases the height of each list item by thirty percent
$movieList.find("li").each(function(index, element) {
var $element = $(element);
$element.css("height", ($element.offset().height * 1.3) + "px");
});
}
};发布于 2014-08-06 16:32:37
使用html追加如下:
$('#movie-list').append('<li style="background:url(http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185'+row.poster_path+') no-repeat; height:30%;"><a href="" data-id="' + row.id + '"><h3>' + row.title + '</h3><p>' + row.vote_average + '/10</p></a></li>');https://stackoverflow.com/questions/25165193
复制相似问题