Make a div into a link

后端 未结 27 1679
栀梦
栀梦 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 04:15

    While I don't recommend doing this under any circumstance, here is some code that makes a DIV into a link (note: this example uses jQuery and certain markup is removed for simplicity):

    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    
    $(document).ready(function() {
        $("div[href]").click(function () {
            window.location = $(this).attr("href");
        });
    });
    
    </script>
    <div href="http://www.google.com">
         My Div Link
    </div>
    
    0 讨论(0)
  • 2020-11-22 04:16
    <a href="…" style="cursor: pointer;"><div> … </div></a>
    
    0 讨论(0)
  • 2020-11-22 04:16

    This work for me:

    <div onclick="location.href='page.html';"  style="cursor:pointer;">...</div>
    
    0 讨论(0)
  • 2020-11-22 04:16

    I pulled in a variable because some values in my link will change depending on what record the user is coming from. This worked for testing :

       <div onclick="location.href='page.html';"  style="cursor:pointer;">...</div> 
    

    and this works too :

       <div onclick="location.href='<%=Webpage%>';"  style="cursor:pointer;">...</div> 
    
    0 讨论(0)
  • 2020-11-22 04:17

    This is the simplest way.

    Say, this is the div block I want to make clickable:

    <div class="inner_headL"></div>
    

    So put a href as follows:

    <a href="#">
     <div class="inner_headL"></div>
    </a>
    

    Just consider the div block as a normal html element and enable the usual a href tag.
    It works on FF at least.

    0 讨论(0)
  • 2020-11-22 04:18

    This is an ancient question, but I thought I'd answer it since everyone here has some crazy solutions. It's actually very very simple...

    An anchor tag works like this -

    <a href="whatever you want"> EVERYTHING IN HERE TURNS INTO A LINK </a>
    

    Sooo...

    <a href="whatever you want"> <div id="thediv" /> </a>
    

    Although I'm not sure if this is valid. If that's the reasoning behind spoken solutions, then I apologise...

    0 讨论(0)
提交回复
热议问题