How do I link a hyperlink to an HTML5
  • image t
  • 2019-09-19 17:15发布

    This is a responsive HTML5 gallery that I've been working on recently. I just want each image to link to a different page.

    <li> 
    
    <div data-alt="img03" data-description="<h3>Project</h3>" data-max-width="1800" data-max-height="1350" > 
    <div data-src="images/xxxlarge/1.jpg" data-min-width="1300"></div>
    
    
    </div>
    
    </li>
    
     I know that it should be as simple as adding a <a href="#"></a> but this isn't working. I am open to hear about other options. Thanks.
    

    1条回答
    我只想做你的唯一
    2楼-- · 2019-09-19 17:55

    You can access the values in data-attributes (data-*) using .dataset (currently supported in all major browsers). I.e. to access the value of the attribute data-src of an element, you would write:

    var srcValue = varHoldingElemNode.dataset.src;   // or
    var srcValue = varHoldingElemNode.dataset["src"];
    

    That said, you can use some JS to achieve what (I suppose) you want: turning a <div data-src=<url_to_image>... into an image linking to some page.

    See, also, this short demo.

    查看更多
    登录 后发表回答