javascript - jquery背景图片url失败

标签 javascript jquery

这不起作用......

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

相关文章:

javascript - "Function declarations cannot be nested inside a statement"- IE 处于严格模式错误

javascript - $routeParam 不通过 $resource 工厂的 url 传递变量

javascript - 单击列表中的项目 -> 查看详细信息但没有输出? Firebase.child 失败 : First argument was an invalid path

javascript - 单击时,单击其他内容,防止默认。我如何使用 jQuery 正确地做到这一点?

jquery - 在 jQuery 集合上运行 reduce()

javascript - Firebase数据结构思想

javascript - jquery 滚动插件错误

jquery - jQuery/HTML5 中的动态更新图像旋转器

jquery - 仅使用 CSS 元素如何具有 "100%-50px"的宽度?

jquery - Bootstrap 阻止 jQuery 更改复选框的状态