首页 > 解决方案 > 使用 JavaScript / jQuery 设置后台链接 URL

问题描述

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

<script>

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

</script>

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

标签: javascriptjquerygoogle-tag-manager

解决方案


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

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

推荐阅读