[英]Setting background image dynamically ul/li div element
I am new in website development and trying to figure out how to set image dynamically in ul
/ li
div
element.我是网站开发的新手,并试图弄清楚如何在
ul
/ li
div
元素中动态设置图像。 Below is my code in html file :下面是我在 html 文件中的代码:
<div class="results">
<ul class="row">
<li class="template hidden col-md-3">
<div class="image">
<!--img src='broken_file.png'-->
<data>
<span class="name"></span>
<span data-before='faces' class="faces"></span>
<span data-before='genus' class="genus"></span>
<span data-before='vertices' class="vertices"></span>
</data>
</div>
</li>
</ul>
</div>
And from another js file I am trying to access the 'image' div as从另一个 js 文件我试图访问“图像”div
$(li).find('image').css()
I am trying to set an image url inside css()
, but not sure how I can do it, or even it is possible?我正在尝试在
css()
设置一个图像网址,但不确定我该怎么做,甚至可能吗?
Firstly your jQuery syntax is incorrect.首先,您的 jQuery 语法不正确。
li
should be in quotes as it's a string, and the image
selector requires a preceding .
li
应该用引号引起来,因为它是一个字符串,并且image
选择器需要一个前面的.
as it's a class.因为它是一个类。 Finally, you can set the
background-image
in CSS, like this:最后,您可以在 CSS 中设置
background-image
,如下所示:
$('li').find('.image').css('background-image', 'url("my-image.png")');
1) If you want to insert img tag into div with "image" class - 1)如果你想将 img 标签插入带有“image”类的 div -
$('li').find('.image').append($('<img src="http://fakeimg.pl/250x100/000/">'))
2) If you want to change src attr - 2) 如果你想改变 src attr -
$('li').find('.image img').attr('src','http://fakeimg.pl/250x100/000/">')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.