使用 JavaScript / jQuery 设置背景 link URL

Setting a background link URL using JavaScript / jQuery

我正在尝试使用 jQuery 设置元素背景图像和 link URL。当我执行以下操作时,背景图像有效,但 link 无效:

<script>

$( ".background" ).css("background-image", "url(https://www.example.com/image1.png)", "href(https://www.example.com)");

</script>

我必须为此使用 Javascript,因为我在 GTM 中设置了用于构建图像位置 URL 和 link URL 的变量。

href与css()无关。使用 attr()

$( ".background" ).css("background-image", "url(https://www.example.com/image1.png)")
                  .attr('href','https://www.example.com')