使用JavaScript/jQuery设置后台链接URL
我正在尝试使用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:/
<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')