j查询背景图像 URL 失败

jquery background image url failed

本文关键字:URL 失败 图像 背景 查询      更新时间:2023-09-26

这不起作用....

$("#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%;
}

我的网页

<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编写的,在根文件夹中。

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

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

也许路径可能有问题。如果可以的话,给出绝对路径。即 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 文件中同样的事情是无效

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

 $(".news_all_item li").css("background", "transparent url('../Encounters/LandingPage.PNG') no-repeat");

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

$("#news_all ul li").css("background-image", "transparent url('../images/01.png') no-repeat");

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

试试这个

$( "ul#news_all> li" ).css("background", "transparent url('../images/01.png') no-repeat");