2

这不起作用....

$("#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>
4

6 回答 6

1

也许路径可能有问题。(如果可以,请给出绝对assets/style/images/01.png路径。即)

在 css 文件中../images/01.png可能存在,但从 js 文件../images/01.png中可能不存在

您可能具有类似的文件夹结构。

asset
     |-->style
          |-->css
               |-->mycss.css
          |-->images
               |-->01.png
     |-->js
          |-->myjs.js

所以这里来自 css../images/01.png是有效的,但来自 js 文件同样的事情是无效的

于 2013-09-12T06:43:41.463 回答
1

我认为这是因为相对链接。

我怀疑您的样式表可能在 css 文件夹中;并且 javascript 可能是用你的 html 编写的,它位于根文件夹中。

在样式表中“../images/”是有效的,因为从文件夹 css 中,“../”返回一级(到根文件夹),然后“/images”访问图像文件夹。但是从根文件夹中,您必须省略 ../ (不再需要返回一个级别)。

无论如何,如果您使用相对链接,请记住路径的起点是编写代码的位置。

于 2013-09-12T06:52:41.153 回答
0

使用背景图像而不是背景..

$("#news_all ul li").css("background-image", "transparent url('../images/01.png') no-repeat");
于 2013-09-12T06:35:16.373 回答
0

它应该是background-image,而不是background作为属性键。

于 2013-09-12T06:36:15.600 回答
0

当您尝试设置背景时,您可以通过类选择器“.news_all_item”而不是“#news_all_item”应用

 $(".news_all_item li").css("background", "transparent url('../Encounters/LandingPage.PNG') no-repeat");
于 2013-09-12T06:38:02.430 回答
0

尝试这个

$( "ul#news_all> li" ).css("background", "transparent url('../images/01.png') no-repeat");
于 2013-09-12T06:39:19.737 回答