简体   繁体   English

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

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

im working with wordpress, there is a product section in which i want to put a link on the image also instead of the title of product, so what i want is, i want to make the image div clickable by using the product title link which is in the second link 我正在与wordpress一起工作,有一个产品部分,我想在图像上也放置链接而不是产品标题,所以我想要的是,我想通过使用产品标题链接使图像div可点击在第二个链接中

This is for a wordpress work, i have tried to find the files where i can change the code, but didn't found, so now i want to resolve it using jquery or any method you suggest 这是用于wordpress的工作,我试图找到可以更改代码的文件,但没有找到,所以现在我想使用jquery或您建议的任何方法来解决它

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

Just use on and closest like so: 只需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>

// in Js or Jquery //在Js或Jquery中

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

I would put the href in the data attribute like so: 我会将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