这不起作用......
$("#news_all ul li").css("background", "transparent url('../images/01.png') no-repeat");
但这正在工作
.news_all_item li {
background: url("../images/01.png") no-repeat scroll 0 0 transparent;
margin-bottom: 2%;
}
我的html
<div class="news_all_item" id="news_all">
<ul>
<li><div class="news_item">Lorem Ipsum is simply dummy text of the printing and typesettiLorem Ipsum is simply dummy text</div></li>
<li><div class="news_item">Lorem Ipsum is simply dummy text of the printing and typesettiLorem Ipsum </div></li>
<li><div class="news_item">Lorem Ipsum is simply dummy text of the printing and typesetti</div></li>
</ul>
</div>
最佳答案
我认为这是因为相对链接。
我怀疑你的样式表可能在 css 文件夹中;并且 javascript 可能是在您的 html 中编写的,该 html 位于根文件夹中。
在样式表中“../images/”是有效的,因为从文件夹 css 开始,“../”返回上一级(到根文件夹),然后“/images”访问图像文件夹。但从根文件夹中,您必须省略 ../(不再需要返回上一级)。
无论如何,如果您使用相对链接,请记住路径的起点是编写代码的位置。
关于javascript - jquery背景图片url失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18756905/