javascript - 使用 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 的变量。
解决方案
href
与 css() 无关。使用 attr()
$( ".background" ).css("background-image", "url(https://www.example.com/image1.png)")
.attr('href','https://www.example.com')
推荐阅读
- yaml - 在 Hugo v0.55 的 YAML 文件中获取 Hugo 的版本号
- docker - docker网络驱动程序之间有什么区别?
- angular - Angular HttpClient 订阅无法读取未定义的属性“名称”
- reactjs - 关闭反应模式
- python - 遍历数据框以购买或拒绝股票
- javascript - 无法显示 responseJson 的结果
- regex - 如何删除角色的特定实例,但不是全部?
- javascript - 在数组中迭代一个数组并显示 playerId、playerName 和 playerCategory
- javascript - SignalR v2 Javascript API 文档在哪里?
- r - 配对图未从闪亮下载