Make a div into a link

后端 未结 27 1678
栀梦
栀梦 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:03

    Just have the link in the block and enhance it with jquery. It degrades 100% gracefully for anyone without javascript. Doing this with html isn't really the best solution imho. For example:

    <div id="div_link">
    <h2><a href="mylink.htm">The Link and Headline</a></h2>
    <p>Some more stuff and maybe another <a href="mylink.htm">link</a>.</p>
    </div>
    

    Then use jquery to make the block clickable (via web designer wall):

    $(document).ready(function(){
    
        $("#div_link").click(function(){
          window.location=$(this).find("a").attr("href"); return false;
        });
    
    });
    

    Then all you have to do is add cursor styles to the div

        #div_link:hover {cursor: pointer;}
    

    For bonus points only apply these styles if javascript is enabled by adding a 'js_enabled' class to the div, or the body, or whatever.

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

    Actually you need to include the JavaScript code at the moment, check this tutorial to do so.

    but there is a tricky way to achieve this using a CSS code you must nest an anchor tag inside your div tag and you must apply this property to it,

    display:block;
    

    when you've done that,it will make the whole width area clickable (but within the height of the anchor tag),if you want to cover the whole div area you must set the height of the anchor tag exactly to the height of the div tag,for example:

    height:60px;
    

    this is gonna make the whole area clickable,then you can apply text-indent:-9999px to anchor tag to achieve the goal.

    this is really tricky and simple and it's just created using CSS code.

    here is an example: http://jsfiddle.net/hbirjand/RG8wW/

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

    Requires a little javascript. But, your div would be clickable.

    <div onclick="location.href='http://www.example.com';" style="cursor:pointer;"></div>
    
    0 讨论(0)
  • 2020-11-22 04:08

    This example worked for me:

    <div style="position: relative; width:191px; height:83px;">
        <a href="link.php" style="display:block; width:100%; height:100%;"></a>
    </div>
    
    0 讨论(0)
  • 2020-11-22 04:12

    To make thepeer's answer work in IE 7 and forward, it needs a few tweaks.

    1. IE will not honour z-index if the element is has no background-color, so the link will not overlap parts of the containig div that has content, only the blank parts. To fix this a background is added with opacity 0.

    2. For some reason IE7 and various compatibility modes completely fail when using the span in a link approach. However if the link itself is given the style it works just fine.

    .blockLink  
    {  
        position:absolute;  
        top:0;  
        left: 0;  
        width:100%;  
        height:100%;  
        z-index: 1;  
        background-color:#ffffff;   
        -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";  
        filter: alpha(opacity=0);  
        opacity:0;  
    }
    
    <div style="position:relative">  
        <some content>  
        <a href="somepage" class="blockLink" />  
    <div>
    
    0 讨论(0)
  • 2020-11-22 04:13

    Came here in the hope of finding a better solution that mine, but I don't like any of the ones on offer here. I think some of you have misunderstood the question. The OP wants to make a div full of content behave like a link. One example of this would be facebook ads - if you look, they're actually proper markup.

    For me the no-nos are: javascript (shouldn't be needed just for a link, and very bad SEO/accessibility); invalid HTML.

    In essence it's this:

    • Build your panel using normal CSS techniques and valid HTML.
    • Somewhere in there put a link that you want to be the default link if the user clicks on the panel (you can have other links too).
    • Inside that link, put an empty span tag (<span></span>, not <span /> - thanks @Campey)
    • give the panel position:relative
    • apply the following CSS to the empty span:

      { 
        position:absolute; 
        width:100%;
        height:100%;
        top:0;
        left: 0;
      
        z-index: 1;
      
        /* fixes overlap error in IE7/8, 
           make sure you have an empty gif */
        background-image: url('empty.gif');
      }   
      

      It will now cover the panel, and as it's inside an <A> tag, it's a clickable link

    • give any other links inside the panel position:relative and a suitable z-index (>1) to bring them in front of the default span link
    0 讨论(0)
提交回复
热议问题