Entire div as a link?

后端 未结 8 1356
抹茶落季
抹茶落季 2021-02-04 04:55

I want to use an entire div as a link.. without the use of \"onclick\"

Is this possible with a simple href?

Thanks

相关标签:
8条回答
  • 2021-02-04 04:57

    Wrap an anchor around it.

    <a href="#"><div></div></a>
    
    0 讨论(0)
  • 2021-02-04 04:58

    why not you use javascript framework such as jquery, let the div has no onclick event but declare an event in jquery, such this:

       <script>
             $(function(){ 
               $(".click").click(function{ alert('clicked!'); });
            });        
       </script>
    
       <div class="click"></div>
    

    just my 2 cents. thanks.

    0 讨论(0)
  • 2021-02-04 05:00

    You can accomplish this by creating a small transparent PNG (say, 10px * 10px) and using CSS to set the width and height of the PNG to 100%. Then you can wrap the img tag with your a href tag.

    Here's the HTML:

    <div id="YourHeader">
        <a href="http://example.com"><img src="/path/to/header10x10.png" /></a>
    </div>
    

    Here's the CSS:

    #YourHeader img {
        width: 100%;
        height: 100%; 
    }
    

    This is especially handy if #YourHeader is an empty div whose only purpose is to display a header image via its background-image attribute.

    0 讨论(0)
  • 2021-02-04 05:03

    you can't. but you could use an <a> with style="display:block", wich should behave exactly like a <div>, to replace your <div>

    0 讨论(0)
  • 2021-02-04 05:10

    Per the HTML spec (HTML 4.01, Draft 5 and XHTML 1,1.1) an anchor tag <a> cannot enclose a <div> tag.

    0 讨论(0)
  • 2021-02-04 05:12

    simple answer is no, you can use onclick with css cursor:pointer to get the same functionality, though.

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