Make a div into a link

后端 未结 27 1653
栀梦
栀梦 2020-11-22 03:15

I have a

block with some fancy visual content that I don\'t want to change. I want to make it a clickable link.

I\'m looking for something l

27条回答
  •  粉色の甜心
    2020-11-22 03:55

    The cleanest way would be to use jQuery with the data-tags introduced in HTML. With this solution you can create a link on every tag you want. First define the tag (e.g. div) with a data-link tag:

    Some content in the div which is arbitrary

    Now you can style the div however you want. And you have to create also the style for the "link"-alike behavior:

    [data-link] {
      cursor: pointer;
    }
    

    And at last put this jQuery call to the page:

    $(document).ready(function() {
      $("[data-link]").click(function() {
        window.location.href = $(this).attr("data-link");
        return false;
      });
    });
    

    With this code jQuery applys a click listener to every tag on the page which has a "data-link" attribute and redirects to the URL which is in the data-link attribute.

提交回复
热议问题