我有一堂课:
<section class="banner booking-widget-home" data-path="/content/xxx/yyy/en/jcr:content/bookingwidget" data-bgset="/content/dam/xxx/aa-website/banner/2018/pppp/2018/04/abc%def%20Fare_Web%20Banner.png" style="background-image: url("https://www.abc.in/content/dam/pqr/website/banner/2018/target/2018/04/abc%20Flexi%20Fare_Web%20Banner.png");">
我想在此类上添加href,以便当我单击背景图片时,单击应将我带到href中提到的url。
我尝试使用:
$(".banner.booking-widget-home").attr("href","www.landingpae.com");
但背景图片不可点击。
答案 0 :(得分:1)
由于它不是<a>
标记,因此<section>
将不可单击。代替使用href
,而使用onclick
并使用location.href
:
$(function () {
$(".banner.booking-widget-home").click(function () {
location.href = "https://example.com/";
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section style="background: url('//placehold.it/100?text=Click+Me') center center; width: 100px; height: 100px; cursor: pointer;" class="banner booking-widget-home"></section>