我正在尝试在页面加载时动态更改,背景取决于您所使用的URL。但这对我不起作用。

var camera1 = "url1";
var camera2 = "url2";
var camera3 = "url3";
var currenturl = window.location;
jQuery(function($)
{
  if(camera1 == currenturl)
  {
    $(".breadcrumb-right").css("background", "url(url1.jpg)   !important;");
  }
  else if (camera2 == currenturl)
  {
    $(".breadcrumb-right").css("background-image", "url(url2.jpg)");
  }
  else if (camera3 == currenturl)
  {
    $(".breadcrumb-right").css("background-image", "url(url3.jpg)");
  }
});

最佳答案

也许您可以通过以下方式改进代码:

const cameras = ['url1', 'url2', 'url3'];
const currentUrl = window.location.href;
const el = $(".breadcrumb-right");

if (cameras.includes(currentUrl)) {
  el.css("background-image", `url(${currentUrl})`);
}

10-06 16:09