Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript/jQuery设置后台链接URL_Javascript_Jquery_Google Tag Manager - Fatal编程技术网

使用JavaScript/jQuery设置后台链接URL

使用JavaScript/jQuery设置后台链接URL,javascript,jquery,google-tag-manager,Javascript,Jquery,Google Tag Manager,我正在尝试使用jQuery设置元素背景图像和链接URL。当我执行以下操作时,背景图像起作用,但链接不起作用: <script> $( ".background" ).css("background-image", "url(https://www.example.com/image1.png)", "href(https://www.example.com)"); </script> $(“.background”).css(“背景图像”,“url(https:/

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

<script>

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

</script>

$(“.background”).css(“背景图像”,“url(https://www.example.com/image1.png)“,”href(https://www.example.com)");

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

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


没有“背景链接”这样的东西。你到底想达到什么目的?我假设你想在点击
.background
元素时转到该URL…?你能提供你的前后html吗?谢谢,我也试过了,但也没用?
$( ".background" ).css("background-image", "url(https://www.example.com/image1.png)")
                  .attr('href','https://www.example.com')