繁体   English   中英

我们如何通过使用另一个div的链接使DIV可点击

[英]How we can make a DIV clickable by using the link from another div

我正在与wordpress一起工作,有一个产品部分,我想在图像上也放置链接而不是产品标题,所以我想要的是,我想通过使用产品标题链接使图像div可点击在第二个链接中

这是用于wordpress的工作,我试图找到可以更改代码的文件,但没有找到,所以现在我想使用jquery或您建议的任何方法来解决它

<div class="product"></div>
<div class="product-title">
    <a class="title" href="url">
</div>

只需onclosest使用on就像这样:

$(".product").on("click", function() {
  window.location = $(this).closest(".product-title").find(".title").attr("href");
});
<div class="product"></div>
<div class="product-title">
    <a class="title" href="url" onclick="PerformAction()">
</div>

//在Js或Jquery中

function PerformAction()
{
  // Your Logic to perform further operations
}

我会将href放在data属性中,如下所示:

<div class="product" data-product_url="url"></div>
<script>
    $('.product').on('click', function () {
        window.location = $(this).data('product_url');
    });
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM