JQuery 第一次没有将背景图片设置为 div

标签 jquery css ajax image background

我正在尝试使用 JQuery 将背景图像设置为 div。如果有多个图像,它会起作用,但如果只有一个 img,它就不起作用。

for(var i = 0; i<offers.length; i++)
{
   var offer = offers[i];
   var photo = photos[i];
   $('.img-product').css("background-image", "url("+photo+")");
   $('.img-product').css("background-size", "100% 100%");
   ...
}

我试图在 $('.img-product').css("background-image", "url("+photo+")"); 之前和之后“提醒”照片的值(value); 语句,它工作正常。

这是多于一个offer时的结果

This is the result when there are more then one offer

这是搜索返回单个元素时的结果

This is the result when search returns a single item

最佳答案

var strong = document.createElement("strong");
            var founds_nbr = document.createTextNode(offers.length);
            var founds_txt = document.createTextNode(" offerte trovate");
            strong.appendChild(founds_nbr);
            founds.appendChild(strong);
            founds.appendChild(founds_txt);
            for(var i = 0; i<offers.length; i++)
            {
                var photo = photos[i];
                var offer = offers[i];

                var a = document.createElement("a");
                var article = document.getElementById("offer");
                var img_div = document.createElement("div");
                var container = document.createElement("div");
                var name = document.createElement("h4");
                var price_div = document.createElement("div");
                var offer_price = document.createElement("h1");
                var old_price = document.createElement("span");
                var expired_time = document.createElement("span");
                var title_text_node = document.createTextNode(offer.name);
                var price_text_node = document.createTextNode(offer.price);
                var old_price_text_node = document.createTextNode("35,00");
                var deadline_text_node = document.createTextNode(offer.deadline);


                a.setAttribute("href","ProductPage/"+offer.id_offer);
                /*article.setAttribute("class", "offer clearfix");
                article.setAttribute("id", "offer");*/
                article.setAttribute("style","visibility: visible");
                img_div.setAttribute("class", "img-product");
                container.setAttribute("class", "container-right clearfix");
                name.setAttribute("class", "offer-title");
                price_div.setAttribute("class", "price clearfix");
                offer_price.setAttribute("class", "offer-price");
                old_price.setAttribute("class", "del-price");
                expired_time.setAttribute("class", "expiredtime");
                $('.img-product').css({"background-image":"url("+photo+")", "background-size":"100% 100%"});
                console.log($('.img-product'));
                name.appendChild(title_text_node);

                offer_price.appendChild(price_text_node);

                old_price.appendChild(old_price_text_node);

                expired_time.appendChild(deadline_text_node);
                price_div.appendChild(offer_price);
                price_div.appendChild(old_price);

                container.appendChild(name);
                container.appendChild(price_div);
                container.appendChild(expired_time);

                article.appendChild(img_div);
                article.appendChild(container);

                a.appendChild(article);

                var container = document.getElementById("container");
                container.appendChild(a);
            }

@headmax

关于JQuery 第一次没有将背景图片设置为 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46675521/

相关文章:

javascript - 以 Rails 友好的方式使用 jquery 序列化数组

javascript - 如何绑定(bind)点击来切换复选框?

jquery - 单击时使用新 URL 链接重新绑定(bind)无限滚动插件

javascript - 如何在不知道表单 ID 的情况下访问表单元素?

javascript - CSS 冲突。 CSS 更改了我的 <select> 类

html - 除非更改名称,否则浏览器不会加载优化图像

javascript - 脚本不会对最近创建的元素生效

css - 打印时,Vuetify 应用程序为不可见元素保留空间

java - 如何在jsp中将服务器目录显示为树形?

java - jquery/javascript : download a file from any webserver